Techibee.com

Find the SID of current logged on user using PowerShell

One of the things I like with PowerShell is its ability to use DotNet classes and methods. In this article we will such approach to find out what is the SID of current logged on user account using PowerShell.

DotNet assembly System.DirectoryServices.AccountManagement has a class called UserPrincipal which gives a simple way to get SID of current logged user. There are several other ways to do it but I found this is easiest of all.

Let see how to do this. First we need to add the System.DirectoryServices.AccountManagement assembly to PowerShell session. You can do it by Add-Type cmdlet.

Add-Type -AssemblyName System.DirectoryServices.AccountManagement

Once the assembly added, you can use below one-liner to query the Current User details and one of the property is SID.

[System.DirectoryServices.AccountManagement.UserPrincipal]::Current

Below screen shows the list of properties that this class provides.

Putting all these together, I made a quick PowerShell function that returns the SID of current logged on user.

function Get-CurrentUserSID {            
[CmdletBinding()]            
param(            
)            
            
Add-Type -AssemblyName System.DirectoryServices.AccountManagement            
return ([System.DirectoryServices.AccountManagement.UserPrincipal]::Current).SID.Value            
            
            
}

You can import this function into your PowerShell window and use it. Let me know if you got any questions.

Exit mobile version