Ever needed to get all nested groups a user belongs in Active Directory?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#Get all recursive groups a user belongs. Function Get-ADUserNestedGroups { Param ( [string]$DistinguishedName, [array]$Groups = @() ) #Get the AD object, and get group membership. $ADObject = Get-ADObject -Filter "DistinguishedName -eq '$DistinguishedName'" -Properties memberOf, DistinguishedName; #If object exists. If($ADObject) { #Enummurate through each of the groups. Foreach($GroupDistinguishedName in $ADObject.memberOf) { #Get member of groups from the enummerated group. $CurrentGroup = Get-ADObject -Filter "DistinguishedName -eq '$GroupDistinguishedName'" -Properties memberOf, DistinguishedName; #Check if the group is already in the array. If(($Groups | Where-Object {$_.DistinguishedName -eq $GroupDistinguishedName}).Count -eq 0) { #Add group to array. $Groups += $CurrentGroup; #Get recursive groups. $Groups = Get-ADUserNestedGroups -DistinguishedName $GroupDistinguishedName -Groups $Groups; } } } #Return groups. Return $Groups; } #The user to check. $User = "<SamAccountName/DN/UserPrincipal>"; #Get all groups. $Groups = Get-ADUserNestedGroups -DistinguishedName (Get-ADUser -Identity $User).DistinguishedName; #Output all groups. $Groups | Select-Object Name | Sort-Object -Property Name; |