Skip to content

Instantly share code, notes, and snippets.

@josephkern
Last active August 29, 2015 14:26
Show Gist options
  • Save josephkern/e4d1e8a426ff45cce461 to your computer and use it in GitHub Desktop.
Save josephkern/e4d1e8a426ff45cce461 to your computer and use it in GitHub Desktop.
function Get-ADMemberOf {
# A fast and accurate way of determining all of the groups
# one or more users are part of, especially useful when the groups are nested.
# Example:
# import-module ActiveDirectory
# get-aduser <username> | Get-MemberOf | ft
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]
[string]$DistinguishedName
)
begin {}
process {
$tokens = get-aduser -SearchScope Base -SearchBase $DistinguishedName -Properties tokengroups -LDAPFilter '(objectClass=user)'
$tokens.tokengroups | % {
$objectProperties = @{ "GroupName" = $_.Translate([System.Security.Principal.NTAccount]);
"SamAccountName" = $tokens.SamAccountName }
New-Object -TypeName PSObject -Property $objectProperties
}
}
end {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment