Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Last active February 29, 2024 22:23
Show Gist options
  • Select an option

  • Save figueroadavid/c998f3c8d5e3c309b446c9d0afccb126 to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/c998f3c8d5e3c309b446c9d0afccb126 to your computer and use it in GitHub Desktop.
Retrieve a user's SID without the AD Module
function Get-UserSID {
<#
.SYNOPSIS
Retrieves a user's security identifier without the need for the ActiveDirectory Module
.DESCRIPTION
Retrieves a user's security identifier without the need for the ActiveDirectory Module
.PARAMETER samAccountName
The actual samAccountName for the user whose account the SID should be retrieved.
.PARAMETER Domain
This is the domain of the account to be checked. It defaults to the domain of the user
running the script.
.NOTES
Thanks to @JBorean93 for this greatly simplified version
https://github.com/jborean93
.EXAMPLE
PS C:\> Get-UserSid -samAccountName User1
S-1-5-21-176475294-9861874819-1984720271-198576
#(The SID is completely made up)
#>
[CmdletBinding()]
param(
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$samAccountName,
[parameter(ValueFromPipelineByPropertyName)]
[string]$Domain = $env:USERDOMAIN
)
try {
[System.Security.Principal.NTAccount]::new($Domain, $samAccountName).Translate(
[System.Security.Principal.SecurityIdentifier]).Value
}
catch {
throw 'Unable to locate the user in ActiveDirectory'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment