TIPS: VBScript : Get Logged in username using VBScript.


  • Using Environment Variable

Set wshShell = WScript.CreateObject( “WScript.Shell” )
strUserName = wshShell.ExpandEnvironmentStrings( “%USERNAME%” )
WScript.Echo “User Name: ” & strUserName

  • Using WshNetwork

Set wshNetwork = WScript.CreateObject( “WScript.Network” )
strUserName = wshNetwork.UserName
WScript.Echo “User Name: ” & strUserName

  • Using ADSI (ADSystemInfo)

Set objSysInfo = CreateObject( “ADSystemInfo” )
strUserName = objSysInfo.UserName
WScript.Echo “User Name: ” & strUserName

  • ADSI (WinNTSystemInfo)

Set objSysInfo = CreateObject( “WinNTSystemInfo” )
strUserName = objSysInfo.UserName
WScript.Echo “User Name: ” & strUserName

  • WMI (Win32_ComputerSystem)

Set objWMI = GetObject( “winmgmts:\\.\root\cimv2” )
Set colItems = objWMI.ExecQuery( “Select * from Win32_ComputerSystem” )
For Each objItem in colItems
strUserName = objItem.UserName
Next

 

Advertisements