private/Get-GroupSID.ps1
<#
.SYNOPSIS Retrieves the Security Identifier (SID) of a group. .DESCRIPTION The Get-GroupSID function retrieves the Security Identifier (SID) of a group specified by its name and optional domain. .PARAMETER GroupName Specifies the name of the group. .PARAMETER DomainName Specifies the domain name of the group. If not provided, the current user's domain will be used. .EXAMPLE Get-GroupSID -GroupName "Domain Admins" Retrieves the SID of the "Administrators" group in the current user's domain. .EXAMPLE Get-GroupSID -GroupName "Domain Admins" -DomainName "contoso.com" Retrieves the SID of the "Administrators" group in the "contoso.com" domain. .EXAMPLE Get-GroupSID -GroupName "CONTOSO\Domain Admins" Retrieves the SID of the "Administrators" group in the "contoso.com" domain. .NOTES Function : Get-GroupSID Author : John Billekens Copyright: Copyright (c) De Technische Jongens Version : 1.1 #> function Get-GroupSID { [CmdletBinding()] Param ( [ValidateNotNullOrEmpty()] [String]$GroupName, [String]$DomainName = ${Env:USERDNSDOMAIN} ) if ($GroupName -like '*\*') { $DomainName, $GroupName = $GroupName.Split('\') } elseif ([String]::IsNullOrEmpty($DomainName) -and $GroupName -like '*\*') { $DomainName, $GroupName = $GroupName.Split('\') } $objUser = New-Object System.Security.Principal.NTAccount($DomainName, $GroupName) $strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier]) return $strSID.Value } |