Skip to content

Instantly share code, notes, and snippets.

@ryanvs
Created June 24, 2019 22:01
Show Gist options
  • Save ryanvs/3df0f1373dc68ca32e8fa58713d13245 to your computer and use it in GitHub Desktop.
Save ryanvs/3df0f1373dc68ca32e8fa58713d13245 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Returns the SID for the specified credentials
.DESCRIPTION
Returns the SID for a user name, using System.Security.Principal.NTAccount. If
no UserName is specified, then the current user name is used.
.NOTES
History:
Date Author Description
2019-06-24 rjvs Initial version
#>
[CmdletBinding()]
param(
[string]$UserName, # Optional: User Name (may include domain, if $UserDomain is null/empty)
[string]$UserDomain # Optional: User Domain
)
if ([string]::IsNullOrEmpty($UserName)) {
Write-Verbose "`$UserName is null or empty"
$UserName = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
Write-Verbose "Current UserName: $UserName"
}
if ($UserName.IndexOf('\') -ge 0) {
$parts = $UserName.Split('\')
$UserDomain = $parts[0]
$UserName = $parts[1]
}
Write-Verbose "UserName: $UserName"
Write-Verbose "UserDomain: $UserDomain"
$user = New-Object System.Security.Principal.NTAccount($UserDomain, $UserName)
$sid = $user.Translate([System.Security.Principal.SecurityIdentifier])
@{ UserName = $UserName; UserDomain = $UserDomain; Sid = $sid.Value }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment