VBScript – How to Access Environment Variables -OR- How to Get the Username, Domain, or a User’s SID

Here is a simple example of how to use VBScript to access environment variables, use the variables to obtain a user’s SID, and then print them to the screen.

Set oShell = CreateObject( "WScript.Shell" )
Dim strUsername: strUsername = oShell.ExpandEnvironmentStrings("%USERNAME%")
Dim strDomain: strDomain =oShell.ExpandEnvironmentStrings("%USERDOMAIN%")
Wscript.Echo "Your username is " & strUsername & " and you are a member of domain " & strDomain & "."

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objAccount = objWMIService.Get ("Win32_UserAccount.Name='" & LCase(strUsername) & "',Domain='" & strDomain & "'")
Wscript.Echo objAccount.SID

Leave a Reply