Skip navigation

Getting the Real Workgroup Name in VBScript and PowerShell - 19 Aug 2010

Downloads
125503.zip

If you have scripts that need to run in both domain-based and workgroup-based environments, you might encounter problems using the %USERDOMAIN% environment variable or the UserDomain property exposed by Windows Script Host's (WSH's) WshNetwork object to obtain workgroup names. You can generally work around the problem with Windows Management Instrumentation (WMI).

The problem is that if a computer isn't logged onto a domain, the %USERDOMAIN% variable and the UserDomain property don't return the computer's workgroup membership. Instead, they return the name of the local computer. This isn't a bug or oversight. The %USERDOMAIN% variable and UserDomain property relate to the security domain and not the name used for grouping computers. Because the security authority for a standalone computer in a workgroup is the local computer itself, the value is correct. Furthermore, in credentials used on standalone systems, the local host name is used in the role of the domain name, making the computer name the correct value to return.

However, when you're dealing with sharing issues in a workgroup-based environment (where the computers are not actually members of a domain), you might need the real workgroup name. For that purpose, your best bet is to use WMI's Win32_ComputerSystem class. Its Domain property specifies the actual domain or workgroup name for the computer.

The code at callout A in Listing 1 demonstrates how to use the Domain property to retrieve and display the local computer's workgroup name in VBScript code.

Listing 1: VBScript Code That Displays Then Changes
the Local Computer's Workgroup Name

' BEGIN CALLOUT A
Dim result, results, domain
Set results = GetObject("winmgmts:"). _
  execquery("select domain from win32_computersystem")
For Each result in results
  ' Returns the workgroup name if in a workgroup.
  ' If a domain member, returns the DNS domain name.
  domain = result.domain
Next
WScript.Echo domain
' END CALLOUT A

' BEGIN CALLOUT B
For Each result in results
  ' On Vista and later, only works if script runs elevated.
  results.JoinDomainOrWorkgroup("Workgroup")
Next
' END CALLOUT B

You can do the same thing in PowerShell with code such as

(Get-WmiObject Win32_ComputerSystem).domain


Note that this technique doesn't produce the same result if you happen to run it on a computer within a domain. In a domain, the Win32_ComputerSystem's domain property is the name of the computer's DNS domain.

The Win32_ComputerSystem class also has the JoinDomainOrWorkgroup method that you can use to set the workgroup name for individual computers. In a workgroup environment, it's very easy to use. All you need to do is specify the new workgroup name. Note that on Windows Vista or later systems, you need elevated privileges to use the JoinDomainOrWorkgroup method.

The code at callout B in Listing 1 demonstrates how to use the JoinDomainOrWorkgroup method to change the workgroup name in VBScript. In PowerShell, you can run code such as

(Get-WmiObject Win32_ComputerSystem).JoinDomainOrWorkgroup("Wkgp")


Before using either the VBScript or PowerShell code, you'd need to replace Wkgp with your workgroup's name. Although it can take a few minutes for the workgroup change information to propagate on a network, the change will take effect immediately on the PC without a reboot.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish