How to get the current user SID in VBS

Here's a little vbscript to read the current user SID. I know it may have been quicker to use the ADSI WinNT provider instead of WMI but the WinNT provider doesn't return the user's SID in a humain readable form.
Function GetCurrentUserSID

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ComputerName = "."

Dim WshNetwork, objWMIService, colItems, objItem
Dim strUserDomain, strUserName

Set WshNetwork = WScript.CreateObject("WScript.Network")
strUserDomain = WshNetwork.UserDomain
strUserName = WshNetwork.UserName

Set objWMIService = GetObject("winmgmts:\\" & ComputerName & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount "&_
       "WHERE (Domain="""&strUserDomain&""" AND Name="""&strUserName&""")", "WQL", _
       wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
     GetCurrentUserSID = objItem.SID
Next

Set colItems = Nothing
Set objWMIService = Nothing
Set WshNetwork=Nothing

End Function
The function should return a string that look like "S-1-5-21-1106520001-3238835185-2771580065-500". This is a SID.
Next PostNewer Post Previous PostOlder Post Home