{"id":16841,"date":"2010-10-12T00:01:00","date_gmt":"2010-10-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/12\/use-powershell-to-translate-a-users-sid-to-an-active-directory-account-name\/"},"modified":"2010-10-12T00:01:00","modified_gmt":"2010-10-12T00:01:00","slug":"use-powershell-to-translate-a-users-sid-to-an-active-directory-account-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-translate-a-users-sid-to-an-active-directory-account-name\/","title":{"rendered":"Use PowerShell to Translate a User&#8217;s SID to an Active Directory Account Name"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell to translate a user&#8217;s SID to an Active Directory Domain Services account name.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" border=\"0\" align=\"left\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" height=\"34\" title=\"Hey, Scripting Guy! Question\" \/>Hey, Scripting Guy! It seems that whenever I search for Windows PowerShell scripts to translate a user name into a SID, all I can find is a script that uses WMI. WMI is too slow for our network. Is there a better way to do this?<\/p>\n<p>&#8212; KW<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" border=\"0\" align=\"left\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" height=\"34\" title=\"Hey, Scripting Guy! Answer\" \/> Hello KW, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. You can easily use the .NET Framework classes in a Windows PowerShell script to translate a user name to a security identifier (SID). In addition, you can use a .NET Framework class to translate a SID to a user name, or you can simply take the SID and use LDAP to retrieve the user name. I will talk about all these techniques in today&rsquo;s article. I created the UserToSid-SidToUser.ps1 script to illustrate these techniques. The complete script is shown here. <\/p>\n<blockquote>\n<p style=\"background: none transparent scroll repeat 0% 0%;margin: 15pt 12pt 0pt 0in;line-height: 11pt;list-style-type: disc\" class=\"CodeBlockScreenedHead\"><span style=\"font-family: Arial\"><span style=\"font-size: 10pt;color: #000000\"><strong>UserToSid-SidToUser.ps1<\/strong><\/span><\/span><\/p>\n<p style=\"background: none transparent scroll repeat 0% 0%;margin: 6pt 17.3pt 0.25in 0in;line-height: 10pt;list-style-type: disc\" class=\"CodeBlockScreened\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">&lt;#           <br \/><span>&nbsp;<\/span><span>&nbsp; <\/span>.Synopsis            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Translates a user name to a SID or a SID to a user name.             <br \/><span>&nbsp;&nbsp; <\/span>.Description            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>This script translates a user name to a SID or a SID to a user name.             <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Note: To translate the user name to the SID, you must            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>use the logon name (SAMAccountName), and not the full user name.            <br \/><span>&nbsp;&nbsp; <\/span>.Example            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>UserToSid.ps1<span>&nbsp; <\/span>-user &#8220;mytestuser&#8221;            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Displays SID of mytestuser in current domain            <br \/><span>&nbsp;&nbsp; <\/span>.Example            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>UserToSid.ps1<span>&nbsp; <\/span>-sid &#8220;S-1-5-21-1877799863-120120469-1066862428-500&#8221;            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>Displays user with SID of &#8220;S-1-5-21-1877799863-120120469-1066862428-500&#8221;            <br \/><span>&nbsp;&nbsp; <\/span>.Inputs            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp; <\/span>.OutPuts            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp; <\/span>.Notes            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>NAME:<span>&nbsp; <\/span>UserToSid-SidToUser.ps1            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>AUTHOR: Ed Wilson            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>LASTEDIT: 10\/05\/2010            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>VERSION: 2.0            <br \/><span>&nbsp;&nbsp;&nbsp; <\/span>KEYWORDS: Active Directory, user accounts, Security.Principal.SecurityIdentifier            <br \/><span>&nbsp;&nbsp; <\/span>.Link            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Http:\/\/www.ScriptingGuys.com            <br \/>#Requires -Version 2.0            <br \/>#&gt;            <br \/>param(            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$domain = $env:USERDOMAIN,            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$user,            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$sid            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>) #end param            <\/p>\n<p># Begin Functions            <\/p>\n<p>function New-Underline            <br \/>{            <br \/>&lt;#            <br \/>.Synopsis            <br \/><span>&nbsp;<\/span>Creates an underline the length of the input string            <br \/>.Example            <br \/><span>&nbsp;<\/span>New-Underline -strIN &#8220;Hello world&#8221;            <br \/>.Example            <br \/><span>&nbsp;<\/span>New-Underline -strIn &#8220;Morgen welt&#8221; -char &#8220;-&#8221; -sColor &#8220;blue&#8221; -uColor &#8220;yellow&#8221;            <br \/>.Example            <br \/><span>&nbsp;<\/span>&#8220;this is a string&#8221; | New-Underline            <br \/>.Notes            <br \/><span>&nbsp;<\/span>NAME:            <br \/><span>&nbsp;<\/span>AUTHOR: Ed Wilson            <br \/><span>&nbsp;<\/span>LASTEDIT: 5\/20\/2009            <br \/><span>&nbsp;<\/span>VERSION: 2.0            <br \/><span>&nbsp;<\/span>KEYWORDS: scripting techniques, string manipulation            <br \/>.Link            <br \/><span>&nbsp;<\/span>Http:\/\/www.ScriptingGuys.com            <br \/>#&gt;            <br \/>[CmdletBinding()]            <br \/>param(            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[Parameter(Mandatory = $true,Position = 0,valueFromPipeline=$true)]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$strIN,            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$char = &#8220;=&#8221;,            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$sColor = &#8220;Green&#8221;,            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[string]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$uColor = &#8220;darkGreen&#8221;,            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[switch]            <br \/><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$pipe            <br \/><span>&nbsp;<\/span>) #end param            <br \/><span>&nbsp;<\/span>$strLine= $char * $strIn.length            <br \/><span>&nbsp;<\/span>if(-not $pipe)            <br \/><span>&nbsp; <\/span>{            <br \/><span>&nbsp;&nbsp; <\/span>Write-Host -ForegroundColor $sColor $strIN            <br \/><span>&nbsp;&nbsp; <\/span>Write-Host -ForegroundColor $uColor $strLine            <br \/><span>&nbsp; <\/span>}            <br \/><span>&nbsp; <\/span>Else            <br \/><span>&nbsp; <\/span>{            <br \/><span>&nbsp;&nbsp; <\/span>$strIn            <br \/><span>&nbsp;&nbsp; <\/span>$strLine            <br \/><span>&nbsp; <\/span>}            <br \/>} #end New-Underline function            <\/p>\n<p>Function Get-UserToSid()            <br \/>{            <br \/><span>&nbsp; <\/span>$ntAccount = new-object System.Security.Principal.NTAccount($domain, $user)            <br \/><span>&nbsp; <\/span>$sid = $ntAccount.Translate([System.Security.Principal.SecurityIdentifier])            <br \/><span>&nbsp; <\/span>New-UnderLine(&#8220;$domain\/$user sid is:&#8221;)            <br \/><span>&nbsp; <\/span>($local:sid).value            <br \/><span>&nbsp; <\/span>exit            <br \/>} #end UserToSid            <\/p>\n<p>Function Get-SidToUser()            <br \/>{            <br \/><span>&nbsp;<\/span>New-Underline(&#8220;Obtaining SID translation &#8230; this might take a bit of time &#8230;&#8221;)            <br \/><span>&nbsp;<\/span>New-UnderLine(&#8220;Sid: $sid is:&#8221;)            <br \/><span>&nbsp;<\/span>[adsi]&#8221;LDAP:\/\/&lt;SID=$sid&gt;&#8221;            <br \/><span>&nbsp;<\/span>exit            <br \/>} #end sidToUser            <\/p>\n<p># *** Entry point to script ***            <\/p>\n<p>if($sid)<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{ Get-SidToUser }            <br \/>if($user)<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>{ Get-UserToSid }<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The UserToSid-SidToUser.ps1 script begins with a comment block. This block uses comment-based help to provide two things to our script. It contains the normal header we would put in a comment block at the top of our script. This includes the author of the script, the name of the script, when the script was written, notes about any special features, what the script uses, and special requirements. These are the sort of things you would want to include for any script you wrote, whether the language is batch, VBScript, Perl, Rexx, Jscript, or Windows PowerShell. In Windows PowerShell 1.0, if you wanted to make a multiline comment, you had to place a number character (<strong>#<\/strong>) at the beginning of each line. I still do this when I want my comments to stand out. A better way to do this, however, in Windows PowerShell 2.0 is to use the multiline comment characters. <\/p>\n<p>I talked about adding help to a Windows PowerShell script the week of January 4, 2010: <\/p>\n<ul>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/04\/hey-scripting-guy-january-4-2010.aspx\">How Can I Add Help to a Windows PowerShell Script?<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/05\/hey-scripting-guy-january-5-2010.aspx\">How Can I Add Multiline Comments to My Windows PowerShell Scripts?<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/06\/hey-scripting-guy-january-6-2010.aspx\">What Should I Include in the Windows PowerShell Script Help?<\/a><\/li>\n<li><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/01\/07\/hey-scripting-guy-january-7-2010.aspx\">How Do I Add Help Information for Windows PowerShell Parameters?<\/a><\/li>\n<\/ul>\n<p>I added the comment block at the beginning of this script by using the technique outlined in the Weekend Scripter article, <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/09\/11\/automatically-add-comment-based-help-to-your-powershell-scripts.aspx\">Automatically Add Comment-Based Help to Your PowerShell Scripts<\/a>.<\/p>\n<p>The header portion of our script is, therefore, contained in a multiline comment block. This comment block is shown here:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">&lt;#<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Synopsis<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Translates a user name to a SID or a SID to a user name. <\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Description<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">This script translates a user name to a SID or a SID to a user name. <\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Note: To translate the user name to the SID, you must<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">use the logon name (SAMAccountName), and not the full user name.<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Example<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">UserToSid.ps1<span>&nbsp; <\/span>-user &#8220;mytestuser&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Displays SID of mytestuser in current domain<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Example<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">UserToSid.ps1<span>&nbsp; <\/span>-sid &#8220;S-1-5-21-1877799863-120120469-1066862428-500&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Displays user with SID of &#8220;S-1-5-21-1877799863-120120469-1066862428-500&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Inputs<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">[string]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.OutPuts<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">[string]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Notes<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">NAME:<span>&nbsp; <\/span>UserToSid-SidToUser.ps1<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">AUTHOR: Ed Wilson<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">LASTEDIT: 10\/05\/2010<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">VERSION: 2.0<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">KEYWORDS: Active Directory, user accounts, Security.Principal.SecurityIdentifier<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">.Link<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Http:\/\/www.ScriptingGuys.com<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">#Requires -Version 2.0<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">#&gt;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Because I followed the rules for adding comment-based help, the header serves two purposes: It documents the script, and allows me to get help directly from the command line. This means I do not have to open the script in the Windows PowerShell ISE or some other script editor just to see what the script does. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2474.HSG-10-12-10-01_107A7E99.jpg\"><img decoding=\"async\" border=\"0\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4431.HSG-10-12-10-01_thumb_0665DD6E.jpg\" alt=\"Image of getting help directly from the command line\" height=\"376\" style=\"border-right: 0px;padding-right: 0px;border-top: 0px;padding-left: 0px;border-left: 0px;padding-top: 0px;border-bottom: 0px\" title=\"Image of getting help directly from the command line\" \/><\/a><\/p>\n<p>The cool thing is that when using comment-based help, it completely integrates with the Windows PowerShell help system via the <strong>Get-Help<\/strong> cmdlet. This means that if I only want to see a sample of the syntax, I use <strong>Get-Help c:\\fso\\UserToSid-SidToUser.ps1 &ndash;Examples<\/strong>. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4861.HSG-10-12-10-02_0BD44E12.jpg\"><img decoding=\"async\" border=\"0\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5432.HSG-10-12-10-02_thumb_582BE1B3.jpg\" alt=\"Image of getting sample syntax\" height=\"376\" style=\"border-right: 0px;padding-right: 0px;border-top: 0px;padding-left: 0px;border-left: 0px;padding-top: 0px;border-bottom: 0px\" title=\"Image of getting sample syntax\" \/><\/a><\/p>\n<p>Please do not think that a command such as <span style=\"font-family: 'Courier New';font-size: x-small\">Get-Help c:\\fso\\UserToSid-SidToUser.ps1 &ndash;Examples<\/span> is too much typing. I use tab completion to type those type of things. Here is what I actually typed (keep in mind that &lt;tab&gt; is pressing the tab key, not actually typing left angle bracket t a b and right angle bracket). <\/p>\n<p><span style=\"font-family: 'Courier New';font-size: x-small\">get-h &lt;tab&gt; c:\\f &lt;tab&gt; usert &lt;tab&gt; -e &lt;tab&gt;<\/span><\/p>\n<p>When typing a command from the Windows PowerShell command line, all you have to type is enough of the command to distinguish it from other commands. If you are not sure how much that is, and you type a command and press Tab, and if the command that appears is not what you want, press Tab again. It means that there were several commands that would match. For example, if you were to type only the letter <strong>g<\/strong> and press Tab, eventually you would get to the command you want to run. The advantage of this is that most Windows PowerShell commands are readable. If you find yourself typing a command over and over, and the use of Tab completion is still too much for you, you can create an alias to the command. The cool thing is that you can also create functions, and then create an alias for your functions. <\/p>\n<p>By creating custom functions and custom aliases, and perhaps storing your aliases in your Windows PowerShell profile, you can make Windows PowerShell work the way you want it to. I have written more than a dozen Hey, Scripting Guy! Blog posts that talk about working with the Windows PowerShell profile. You can use <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=powershell%20profile&amp;sections=7618\">this search string<\/a> to get started reviewing my articles about using the Windows PowerShell profile. In <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/11\/10\/hey-scripting-guy-how-can-i-create-a-custom-function.aspx\">How Can I Create a Custom Function?<\/a>, I talk about modifying the behavior of an existing Windows PowerShell cmdlet and then creating a custom alias for it. <\/p>\n<p>The script creates three parameters. One parameter, <strong>$domain<\/strong>, pulls the user&rsquo;s domain information from the environment variables. If you wish to query a different domain, you will need to supply a different value when calling the script. The other two parameters, <strong>$sid<\/strong> and <strong>$user<\/strong>, take effect only when they are present:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">param(<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">[string]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$domain = $env:USERDOMAIN,<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">[string]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$user,<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">[string]<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$sid<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">) #end param<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>This script does not accept an array of users for input, and it will generate the error seen here if you attempt to supply an array of users to it:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt; $a = &#8220;ed&#8221;,&#8221;teresa&#8221;<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt; C:\\fso\\UserToSid-SidToUser.ps1 -user $a<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">Exception calling &#8220;Translate&#8221; with &#8220;1&#8221; argument(s): &#8220;Some or all identity references<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">could not be translated.&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">At C:\\fso\\UserToSid-SidToUser.ps1:89 char:30<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">+<span>&nbsp;&nbsp; <\/span>$sid = $ntAccount.Translate &lt;&lt;&lt;&lt; ([System.Security.Principal.SecurityIdentifier]<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">)<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">+ CategoryInfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>: NotSpecified: (:) [], MethodInvocationException<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">+ FullyQualifiedErrorId : DotNetMethodException<\/span><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>On the other hand, if you use the pipeline and the <strong>ForEach-Object<\/strong> cmdlet (with the alias, <strong>%<\/strong>), you can easily work around this limitation. This is illustrated here:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt; $a = &#8220;ed&#8221;,&#8221;teresa&#8221;<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt; $a | % { C:\\fso\\UserToSid-SidToUser.ps1 -user $_ }<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">NWTRADERS\/ed sid is:<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">====================<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">S-1-5-21-3746122405-834892460-3960030898-1115<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">NWTRADERS\/teresa sid is:<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">========================<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">S-1-5-21-3746122405-834892460-3960030898-1207<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Because you can supply an array of user names via the pipeline, it means that the <strong>Get-Content<\/strong> cmdlet can be used to read a text file, such as the users.txt file shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4454.HSG-10-12-10-04_68C3DCA1.jpg\"><img decoding=\"async\" border=\"0\" width=\"233\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7026.HSG-10-12-10-04_thumb_27492A48.jpg\" alt=\"Image of users.txt file\" height=\"203\" style=\"border-right: 0px;padding-right: 0px;border-top: 0px;padding-left: 0px;border-left: 0px;padding-top: 0px;border-bottom: 0px\" title=\"Image of users.txt file\" \/><\/a><\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt; $a = Get-Content C:\\fso\\users.txt<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt; $a | % { C:\\fso\\UserToSid-SidToUser.ps1 -user $_ }<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">NWTRADERS\/ed SID is:<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">====================<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">S-1-5-21-3746122405-834892460-3960030898-1115<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">NWTRADERS\/teresa SID is:<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">========================<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">S-1-5-21-3746122405-834892460-3960030898-1207<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">NWTRADERS\/bob SID is:<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">=====================<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">S-1-5-21-3746122405-834892460-3960030898-3601<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">NWTRADERS\/administrator SID is:<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">===============================<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">S-1-5-21-3746122405-834892460-3960030898-500<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">PS C:\\&gt;<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>I use the <strong>System.Security.Principal.NTAccount<\/strong> .NET Framework class to perform the translation to SID. The <strong>NTAccount<\/strong> class resides in the <strong>System.Security.Principal<\/strong> .NET Framework namespace. When calling the <strong>translate<\/strong> method from the <strong>NTAccount<\/strong> class,I tell it that I want to translate the account name to a security identifier by specifying the <strong>System.Security.Principal.SecurityIdentifier<\/strong> class as type to translate. This is shown here:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">Function Get-UserToSid()<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">{<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$ntAccount = new-object System.Security.Principal.NTAccount($domain, $user)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$sid = $ntAccount.Translate([System.Security.Principal.SecurityIdentifier])<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">New-UnderLine(&#8220;$domain\/$user sid is:&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">($local:sid).value<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">exit<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">} #end UserToSid<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>I could use the <strong>SecurityIdentifier<\/strong> .NET Framework class to translate from SID to user name. If I did, the code would look something like the following:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">Function Sid-toUser<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">{<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">$sidString = &#8220;S-1-5-21-3746122405-834892460-3960030898-500&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">$sid = new-object System.Security.Principal.SecurityIdentifier($sidString)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">$user = $sid.Translate([System.Security.Principal.NTAccount])<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">$user.value<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>I decided to not do this, but rather to perform a direct LDAP call instead. The advantage of this is that it returns a <strong>DirectoryEntry<\/strong> class that can be easily used to perform user manipulation. The <strong>SidtoUser<\/strong> function shown above (in addition to not possessing a proper Windows PowerShell function name) only returns a string, and would therefore require additional processing to return a <strong>DirectoryEntry<\/strong> object. <\/p>\n<p>The <strong>Get-SidToUser<\/strong> function, shown here, relies on the fact that the LDAP can return an object based upon its SID. This technique could be used via VBScript as well, because it is basic LDAP stuff. The <strong>Get-SidToUser<\/strong> function is shown here:<\/p>\n<blockquote>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">Function Get-SidToUser()<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">{<\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">New-Underline(&#8220;Obtaining SID translation &#8230; this might take a bit of time &#8230;&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">New-UnderLine(&#8220;Sid: $sid is:&#8221;)<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">[adsi]&#8221;LDAP:\/\/&lt;SID=$sid&gt;&#8221;<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">exit<\/span><\/span><\/span><\/span><\/p>\n<p style=\"margin: 4pt 0in 7pt;line-height: 10pt;list-style-type: disc\" class=\"CodeBlock\"><span><span style=\"font-family: 'Lucida Sans Typewriter'\"><span style=\"font-size: 10pt;color: #000000\">} #end get-sidToUser<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>KW, that is all there is to translating a user account name to a SID, and converting a SID back to a user account name. Active Directory Week will continue tomorrow. <\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell to translate a user&#8217;s SID to an Active Directory Domain Services account name. &nbsp; Hey, Scripting Guy! It seems that whenever I search for Windows PowerShell scripts to translate a user name into a SID, all I can find is a script [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,51,3,4,20,45],"class_list":["post-16841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell to translate a user&#8217;s SID to an Active Directory Domain Services account name. &nbsp; Hey, Scripting Guy! It seems that whenever I search for Windows PowerShell scripts to translate a user name into a SID, all I can find is a script [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16841"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}