The quick way to get a user's SID or identify a SID if the username is unknown. First we use the command line to retrieve the reqested values, then a custom PowerShell function that can be used to get any of the two.
CMD
We need to use separate commands to get the two, depends on what we have. If you know the user name, user the first command, if you have the SID, the second command retrieves the user name.
# Get SID: wmic useraccount where name='alice' get sid # Get samaccountname: wmic useraccount where sid='S-1-5-21-3836600947-993137071-1419413811-1153' get name
PowerShell
The script
# Script to get SID from user name and vice versa # # Usage: # To get SamAccountName: GetSid -SID "S-1-5-21-3836600947-993137071-1419413811-1153" # To get SID: GetSid -Domain alwayshotcafe -User alice Function GetSid { Param ($SID, $Domain, $User) If ($SID -ne $null -and $User -ne $null) {Write-Host -f magenta "Please specify ONLY the SID or the user name, not both!"; break} If ($SID -eq $null -and $User -eq $null) {Write-Host -f yellow "Usage:`r`nGetSid -SID 'xxx'`r`nGetSid -Domain 'xxx' -Username 'yyy'`r`n"; break} If ($SID -ne $null) { $sid = New-Object System.Security.Principal.SecurityIdentifier ($SID) $user = $sid.Translate( [System.Security.Principal.NTAccount]) $user.Value } If ($User -ne $null) { # If domain user If ($Domain -ne $null) { $user = New-Object System.Security.Principal.NTAccount($Domain, $User) } else { # If local user $user = New-Object System.Security.Principal.NTAccount($User) } $sid = $user.Translate([System.Security.Principal.SecurityIdentifier]) $sid.Value } }
Comments