 Foreach ($User in $Users){ $DirectGroups = Get-adprincipalgroupmembership $User.SamAccountName $UserName = $User.Name $AllGroups = @() $AllGroupsName = @() $UpperGroups = @() $GroupsTemp = @() $AllGroups += $DirectGroups.DistinguishedName $Groups = $DirectGroups.DistinguishedName do{ foreach ($Group in $Groups){ $UpperGroups = @() $UpperGroups = (Get-ADGroup $Group -Properties MemberOf).MemberOf if ($UpperGroups -ne ""){ $AllGroups += $UpperGroups $GroupsTemp += $UpperGroups } } $Groups = $GroupsTemp $GroupsTemp = @() }while ($Groups -ne "") 