Home » PowerShell » Convert SID to Username in PowerShell and Cmd

Convert SID to Username in PowerShell and Cmd

If you have user SID, we can convert SID to username in PowerShell using the .NET framework class System.Security.Principal.SecurityIdentifier.Use the wmic command to get user from SID in the command line.

Security Identifier (SID) is a unique value assigned to objects to identify a trustee and issued by an authority, such as a Windows Domain controller.

In this example, we will discuss how to convert SID to username in PowerShell using the .NET Framework library and get user by sid in the command line.

PowerShell SID to UserName

Use the .NET Framework class System.Security.Principal.SecurityIdentifier that takes SID ( security identifier) as input.

It translates the SID to username by calling the Translate() method that accepts System.Security.Principal.NTAccount as input.

Refer to the following PowerShell script to convert SID to username.

# Give SID as input to .NET Framework Class
$SID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-21-1326752099-4012446882-462961959-1103")

# Use Translate to find user from sid
$objUser = $SID.Translate([System.Security.Principal.NTAccount])

# Print the converted SID to username value
$objUser.Value

The output of the above PowerShell script to convert SID to username is:

PowerShell SID to UserName
PowerShell SID to UserName

SID to UserName in Command line

You can use the command line (cmd) to convert SID to username using the wmic command. Using the wmic command to get user account, specify the user SID in the where clause to get a user from SID.

wmic useraccount where sid='S-1-5-21-1326752099-4012446882-462961959-1103' get name, caption,FullName

In the above command, it gets the user from SID and returns user object properties like user name, caption, full name, Domain, etc…

The output of the above command to convert SID to username using the command line is:

C:\>wmic useraccount where sid='S-1-5-21-1326752099-4012446882-462961959-1103' get name, caption,FullName

Caption        FullName   Name
SHELLPRO\toms  Tom Smith  toms


C:\>

Conclusion

I hope the above article on how to convert SID to username in PowerShell and command-line is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.