Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Network > Names > Computer Name

Retrieving the Computer Name

In this section I'll show you how to retrieve a computer name using various scripting techniques.

 

Environment Variable
VBScript Code:
Set wshShell = CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
WScript.Echo "Computer Name: " & strComputerName
Requirements:
Windows version: NT 4, 2000, XP, Server 2003, or Vista
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" )
strComputerName = wshNetwork.ComputerName
WScript.Echo "Computer Name: " & strComputerName
Requirements:
Windows version: Windows 98, ME, NT 4, 2000, XP, Server 2003, Vista
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]
 
SHGINA.DLL (Shell.LocalMachine)
VBScript Code:
Set objPC = CreateObject( "Shell.LocalMachine" )
strComputerName = objPC.MachineName
WScript.Echo "Computer Name: " & strComputerName
Requirements:
Windows version: XP (not sure about other Windows versions)
Network: Stand-alone, workgroup, NT domain, or AD
Client software: N/A
Script Engine: any
Summarized: Works in Windows XP, may or may not work in Windows NT 4, 2000, Server 2003 and Vista.
Doesn't work in Windows 95, 98 or ME.
 
[Back to the top of this page]
 
ADSI (WinNTSystemInfo)
VBScript Code:
Set objSysInfo = CreateObject( "WinNTSystemInfo" )
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
Requirements:
Windows version: 2000, XP, Server 2003, or Vista (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" )
strComputerName = objSysInfo.ComputerName
WScript.Echo "Computer Name: " & strComputerName
Requirements:
Windows version: 2000, XP, Server 2003, or Vista (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 stand-alones, workgroup members or members of NT domains.
 
[Back to the top of this page]
 
WMI (Win32_ComputerSystem)
VBScript Code:
Set objWMIService = GetObject( "winmgmts:\\.\root\cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem" )
For Each objItem in colItems
    strComputerName = objItem.Name
    WScript.Echo "Computer Name: " & strComputerName
Next
Requirements:
Windows version: ME, 2000, XP, Server 2003, or Vista (95, 98, NT 4 with WMI CORE 1.5)
Network: Stand-alone, workgroup, NT domain, or AD
Client software: WMI CORE 1.5 for Windows 95, 98 or NT 4
Script Engine: any
Summarized: Can work on any Windows computer, but WMI CORE 1.5 is required for Windows 95, 98 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]
 
System Scripting Runtime
VBScript Code:
Set objLM = CreateObject( "SScripting.LMNetwork" )
strComputerName = objLM.ComputerName
WScript.Echo "Computer Name: " & ComputerName
Requirements:
Windows version: any
Network: NETBIOS
Client software: System Scripting Runtime
Script Engine: any
Summarized: Works in any Windows version with System Scripting Runtime is installed, with any script engine, except maybe Novell networks.
 
[Back to the top of this page]

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