Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell : Converting AccountName to SID and vice versa

I did allready a lot of posts about using the system.security .NET class and methods,

But as I needed to lookup a SID from a security template at work today, and I was on a machine without my Custom Tabcompletion ( See  PowerTab Overview Page ), so I had no Tab completion on .NET classes, it took some time to find the complete classname again and it is a lot of typing without tabcompletion.

Hence, as I need this often, I decided to make to simple utility functions for this, and load them in my profile :

 

function ConvertTo-NtAccount ($sid) {(new-object system.security.principal.securityidentifier($sid)).translate([system.security.principal.ntaccount])}


function ConvertTo-Sid ($NtAccount) {(new-object system.security.principal.NtAccount($NTaccount)).translate([system.security.principal.securityidentifier])}

now you can use them like this :

 

PoSH> ConvertTo-Sid everyone                                                                                            
                                                                                                                        
                           BinaryLength AccountDomainSid                        Value                                   
                           ------------ ----------------                        -----                                   
                                     12                                         S-1-1-0                                 
                                                                                                                        
                                                                                                                        
PoSH> ConvertTo-NtAccount S-1-5-32-544                                                                                  
                                                                                                                        
Value                                                                                                                   
-----                                                                                                                   
BUILTIN\Administrators                                                                                                  
                                                                                                                        
                                                                                                                        
PoSH> 

 

As said I did allready more posts about this topic

So for more information and more (advanced) examples about working with SID's Users and ACL's, 

also see : Posts on my old blog about this . (about 18 posts where I use this class)

 

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, January 23, 2007 1:19 PM by admin
Filed under: , , ,

Comments

# re: PowerShell : Converting AccountName to SID and vice versa

Extremely useful - I just added these to my profile as well. Thanks!

Thursday, May 03, 2007 12:46 PM by George Tsiokos

# re: PowerShell : Converting AccountName to SID and vice versa

I added these functions to profile.ps1 and tried to use them receiving the following error

Cannot overwrite variable NtAccount because it is read-only or constant.

After some research I found out you can set them Constant or Read Only. What I have not figured out is where they are being set.

can anyone enlighten me?

Bill

Tuesday, January 22, 2008 5:24 PM by bfoster68

# re: PowerShell : Converting AccountName to SID and vice versa

Nevermind i figured it out. It's being set in powertab somewhere. I chnaged the variable names and poof it works.

Bill

Tuesday, January 22, 2008 5:31 PM by bfoster68
Anonymous comments are disabled