Last active
August 29, 2015 14:26
-
-
Save josephkern/e4d1e8a426ff45cce461 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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