MyITGuy
3/24/2014 - 8:37 PM

PowerShell: What is my SID?

PowerShell: What is my SID?

(New-Object System.Security.Principal.NTAccount($env:USERNAME)).Translate([System.Security.Principal.SecurityIdentifier]).Value
[Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$SIDs = @()
$SIDs += (& "$($env:windir)\system32\whoami.exe" /user /fo csv | ConvertFrom-Csv).SID
$SIDs += (& "$($env:windir)\system32\whoami.exe" /groups /fo csv | ConvertFrom-Csv).SID
$SIDs
function Get-SidTranlation {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [System.String[]]
        $NTAccount = @([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)
    )

    begin {
    }

    process {
        foreach ($Item In $NTAccount) {
            $Properties = [ordered]@{
                NTAccount          = New-Object System.Security.Principal.NTAccount($Item)
                SecurityIdentifier = $null
                HexString          = $null
            }
            try {
                $Properties.SecurityIdentifier = $Properties.NTAccount.Translate([System.Security.Principal.SecurityIdentifier])
                $bytes = [byte[]]::CreateInstance([byte], $Properties.SecurityIdentifier.BinaryLength)
                $Properties.SecurityIdentifier.GetBinaryForm($bytes, 0)
                $Properties.HexString = "0x$(($bytes | ForEach-Object ToString X2) -join '')"
            }
            catch {}
            [PSCustomObject]$Properties
        }
    }

    end {

    }
}