Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Network > Names > Domain or Workgroup Name

Retrieving the User Domain,
Computer Domain or Workgroup Name

In this section I'll show you how to retrieve the (user or computer) domain or workgroup name using various scripting techniques.

 

Environment Variable
VBScript Code:
Set wshShell = CreateObject( "WScript.Shell" )
strUserDomain = wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
WScript.Echo "User Domain: " & strUserDomain
Requirements:
Windows version: NT 4, 2000, XP, Server 2003, Vista or Server 2008
Network: Stand-alone, workgroup, NT domain, or AD
Client software: N/A
Script Engine: WSH
Summarized: Works in Windows NT 4 or later, *.vbs with CSCRIPT.EXE or WSCRIPT.EXE only.
Doesn't work in Windows 95, 98 or ME, nor in Internet Explorer (HTAs).
 
[Back to the top of this page]
 
WshNetwork
VBScript Code:
Set wshNetwork = CreateObject( "WScript.Network" )
strUserDomain = wshNetwork.UserDomain
WScript.Echo "User Domain: " & strUserDomain
Requirements:
Windows version: Windows 98, ME, NT 4, 2000, XP, Server 2003, Vista, Server 2008
Network: Stand-alone, workgroup, NT domain, or AD
Client software: Windows Script 5.6 for Windows 98, ME, and NT 4 (no longer available for download?)
Script Engine: WSH
Summarized: Works in Windows 98 or later, *.vbs with CSCRIPT.EXE or WSCRIPT.EXE only.
Doesn't work in Windows 95, nor in Internet Explorer (HTAs).
 
[Back to the top of this page]
 
ADSI (WinNTSystemInfo)
VBScript Code:
Set objSysInfo = CreateObject( "WinNTSystemInfo" )
strUserDomain = objSysInfo.DomainName
WScript.Echo "User Domain: " & strUserDomain
Requirements:
Windows version: 2000, XP, Server 2003, Vista or Server 2008 (95, 98, ME, NT 4 with Active Directory client extension)
Network: Stand-alone, workgroup, NT domain, or AD
Client software: Active Directory client extension for Windows 95, 98, ME or NT 4
Script Engine: any
Summarized: Can work in any Windows version, but Active Directory client extension is required for Windows 95, 98, ME or NT 4.
Can be used in *.vbs with CSCRIPT.EXE or WSCRIPT.EXE, as well as in HTAs.
 
[Back to the top of this page]
 
ADSI (ADSystemInfo)
VBScript Code:
Set objSysInfo = CreateObject( "ADSystemInfo" )
strUserDomain = objSysInfo.DomainName
WScript.Echo "User Domain: " & strUserDomain
Requirements:
Windows version: 2000, XP, Server 2003, Vista or Server 2008 (95, 98, ME, NT 4 with Active Directory client extension)
Network: Only AD domain members
Client software: Active Directory client extension for Windows 95, 98, ME or NT 4
Script Engine: any
Summarized: For AD domain members only.
Can work in any Windows version, but Active Directory client extension is required for Windows 95, 98, ME or NT 4 SP4.
Can be used in *.vbs with CSCRIPT.EXE or WSCRIPT.EXE, as well as in HTAs.
Doesn't work on standalones, workgroup members or members of NT domains.
 
[Back to the top of this page]
 
WMI (Win32_ComputerSystem)
VBScript Code:
Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem" )
For Each objItem in colItems
    strComputerDomain = objItem.Domain
    If objItem.PartOfDomain Then
        WScript.Echo "Computer Domain: " & strComputerDomain
    Else
        WScript.Echo "Workgroup: " & strComputerDomain
    End If
Next
Requirements:
Windows version: XP, Server 2003, Vista or Server 2008
Network: Stand-alone, workgroup, NT domain, or AD
Client software: N/A
Script Engine: any
Summarized: Works in Windows XP and later.
Can be used in *.vbs with CSCRIPT.EXE or WSCRIPT.EXE, as well as in HTAs.
 
[Back to the top of this page]
 
WMI (Win32_NTDomain)
VBScript Code:
Set objWMIService = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_NTDomain" )
For Each objItem in colItems
    strComputerDomain = objItem.DomainName
    WScript.Echo "Computer Domain: " & strComputerDomain
Next
Requirements:
Windows version: XP, Server 2003, Vista or Server 2008
Network: NT domain, or AD
Client software: N/A
Script Engine: any
Summarized: Will work only on AD or NT domain members running Windows XP or later.
Can be used in *.vbs with CSCRIPT.EXE or WSCRIPT.EXE, as well as in HTAs.
Doesn't work in Windows 95, 98, ME, NT 4, or 2000.
Doesn't work on standalones or workgroup members.
 
[Back to the top of this page]
 
System Scripting Runtime
VBScript Code:
Set objIP = CreateObject( "SScripting.IPNetwork" )
strComputerDomain = objIP.Domain
WScript.Echo "Computer Domain: " & strComputerDomain
Requirements:
Windows version: any
Network: TCP/IP
Client software: System Scripting Runtime
Script Engine: any
Summarized: Works in any Windows version with System Scripting Runtime is installed, with any script engine.
 
[Back to the top of this page]

page last modified: 2016-09-19; loaded in 0.0023 seconds