Converting a binary type SID to System.Security.Principal.SecurityIdentifier in PowerShell

David picture David · Aug 17, 2012 · Viewed 9.6k times · Source

I found a link on this website which shows how to convert a SID to the binary type. Can someone please tell me how to do the reverse - to convert a binary type back to a SID.

This is the script that was provided to go from SID to binary:

PS> $sid = New-Object System.Security.Principal.SecurityIdentifier ("S-1-5-21-105005785-2143699225-541720777-501") 
PS> $c = New-Object 'byte[]' $sid.BinaryLength 
PS> $sid.GetBinaryForm($c, 0) 

Thanks!

Answer

jon Z picture jon Z · Aug 17, 2012

To go from byte array to SID, try:

(New-Object System.Security.Principal.SecurityIdentifier($c, 0)).toString()