Get Users SID with PowerShell

What is a User SID? It is user’s unique identifier, usually used in application to relate to a user in a unique way. Microsoft defines SID in the following way:

A data structure of variable length that identifies user, group, and computer accounts. Every account on a network is issued a unique SID when the account is first created. Internal processes in Windows refer to an account’s SID rather than the account’s user or group name.

Applications as TFS, MDS, etc use SID to relate to a user in their databases. For various reasons you may need to retrieve the SID for a given user in order to perform the desired operation. There are multiple ways of obtaining it, and what I do often see is leveraging the ActiveDirectory module in order to retrieve such a simple metric. Although ActiveDirectory module is a powerful it is an overkill for this particular task.
There is a much simpler way to retrieve it and that is by leveraging the .Net classes. Following is a call packed in a cmdlet.

function Get-UserSid
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)] [string]$Username
    )
    BEGIN
    {
        Write-Verbose "Entering script $($MyInvocation.MyCommand.Name)"
        Write-Verbose "Parameter Values"
        $PSBoundParameters.Keys | ForEach-Object { Write-Verbose "$_ = '$($PSBoundParameters[$_])'" }
    }
    PROCESS
    {
        if ($Username.Contains("\"))
        {
            $dl = $Username -split "\\"
        
            $domain = $dl[0]
            $user = $dl[1]

            $objUser = New-Object System.Security.Principal.NTAccount($domain, $user)
            $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) 
            
            return $strSID.Value
        }
        else
        {
            throw "Username not in down-level logon name format (DOMAIN\UserName)."
        }
    }
    END { }
}

In case your computer is not part of a domain, you can just pass in as the domain parameter the host name.

This kind of information is relatively easy to find, I still wrote this as a reminder for myself.

Still, I hope it can help someone.

Have your own printed invitations

For your invitations we offer a number of options:

Paper – Many people use a high quality cardstock paper that is fine for most invitations. We offer a number of cardstock options. Some choose to not use cardstock, and save money and weight. If you’re looking for something else, you may also want to consider the high quality photo paper options available from us in our gift wrap and gift wrap collection.

Many people use a high quality cardstock paper that is fine for most invitations. We offer a number of cardstock options. Some choose to not use cardstock, and save money and weight. If you’re looking for something else, you may also want to consider the high quality photo paper options available from us in our gift wrap and gift wrap collection. PDF The “official” form for this card is a PDF. All other file types require a program, such as Adobe Photoshop, to open and print on. A note about the PDF file type: you can open this file with any program that can open a PDF file, such as Adobe Photoshop, Word, or even Google Drive. The file is free to download, even if you don’t buy anything. In the past, the cost to print our printed invitation designs was much more, due to the high cost to print a card (including production costs, but especially for envelopes). Please don’t hesitate to ask us if you need any more information on how to send the card to your loved ones. Thanks for joining us in being able to share this magical love letter!

Love Letter: A Valentine’s Day Card for Friends & Family A beautiful Valentine’s Day gift that is both personal and thoughtful. Gift Print PDF Love Letter Print A gorgeous Valentine’s Day gift that is both personal and thoughtful, all you need to do is to make sure to use the best custom label printing services. You can get more information about your printing options over here.