Monday, August 11, 2014

Find User SID using Powershell

These snippets allow to find a local or AD user SID or get the username from the SID
<#
.SYNOPSIS
Finds SID for a local user account.
.DESCRIPTION
Returns the SID of a local user account. To find the SID for an active directory user use
findADSID instead
.PARAMETER $user
The user for which to find the SID
.EXAMPLE
PS C:\> findLocalSID -user Guest
.INPUTS
System.String
.OUTPUTS
System.String
.LINK
about_functions_advanced
.LINK
about_comment_based_help
#>
function findLocalSID($user) {
$Objuser = New-Object System.Security.Principal.NTAccount($user)
$strSID = $Objuser.Translate([System.Security.Principal.SecurityIdentifier])
return $strSID.Value
}
<#
.SYNOPSIS
finds the SID for an Active directory user
.DESCRIPTION
returns a string in the form of an sid
.PARAMETER $domain
the domain the user belongs to
.PARAMETER $user
the domain user
.EXAMPLE
PS C:\> findADSID -domain <domain> -user <user>
.INPUTS
System.String
.OUTPUTS
System.String
.LINK
about_functions_advanced
.LINK
about_comment_based_help
#>
function findADSID{
param(
[Parameter(Mandatory = $true)]
$domain,
[Parameter(Mandatory = $true)]
$user)
$Objuser = New-Object System.Security.Principal.NTAccount($domain,$user)
$strSID = $Objuser.Translate([System.Security.Principal.SecurityIdentifier])
return $strSID.Value
}
<#
.SYNOPSIS
find User id based on SID Value
.DESCRIPTION
returns user name
.PARAMETER $sid
sid value (not quoted)
.EXAMPLE
PS C:\> findUserFromSID S-1-5-21-2213100911-2073097868-631715938-506
.INPUTS
System.String
.OUTPUTS
System.String
.NOTES
Additional information about the function goes here.
.LINK
about_functions_advanced
.LINK
about_comment_based_help
#>
function findUserFromSID{
param(
[Parameter(Mandatory = $true)]
$sid
)
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid)
$Objuser = $objSID.Translate([system.Security.Principal.NTAccount])
return $Objuser.Value
}

No comments:

Post a Comment