Convert Username to SID using VBScript

We can easily convert username to sid by using the WMI service class Win32_UserAccount with VBScript.

1. Copy the below example vb script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ConvertUsertoSID.vbs.
3. Double-click the vbscript file (or Run this file from command window).
4. Enter username and domain name and click Enter to resolve username to sid.

Option Explicit
Dim objWMIService,objAccount 
Dim strUserName,strSID,strDomainName,server

' Asks username and domain name from user.
strUserName = InputBox ("Please enter user name")
strDomainName = InputBox ("Please enter domain name")

  server = "."
  Set objWMIService = GetObject("winmgmts:" & server & "rootcimv2") 
  Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUserName & "',Domain='" & strDomainName & "'")
  If Err.Number <> 0 Then
       Msgbox Err.Description
        Err.Clear
  Else
    Msgbox "User SID: " & vbtab & objAccount.SID & vbcrlf
  End If
Advertisement

1 thought on “Convert Username to SID using VBScript”

Leave a Comment