Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Network > Names > User Name

Retrieving the User Name

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

 

Environment Variable
VBScript Code:
Set wshShell = CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
WScript.Echo "User Name: " & strUserName
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" )
strUserName = wshNetwork.UserName
WScript.Echo "User Name: " & strUserName
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]
 
ADSI (WinNTSystemInfo)
VBScript Code:
Set objSysInfo = CreateObject( "WinNTSystemInfo" )
strUserName = objSysInfo.UserName
WScript.Echo "User Name: " & strUserName
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" )
strUserName = objSysInfo.UserName
WScript.Echo "User Name: " & strUserName
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
    strUserName = objItem.UserName
    WScript.Echo "User Name: " & strUserName
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 objSys = CreateObject( "SScripting.System" )
strUserName = objSys.UserName
WScript.Echo "User Name: " & strUserName
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.0019 seconds