Link to home
Start Free TrialLog in
Avatar of Lucky Tham
Lucky Tham

asked on

Find AD Nested Groups

Hi,

Anyone can advice how to modify below script to find few hundred nested groups?

Tks.

Lucky  

Function Test-ADGroupMemberInfra  {
                Param ($User)

                Trap {Return "error"}
                $output= $User
                $output+= " * "
               
If (Get-ADUser -Filter "memberOf -RecursiveMatch '$((Get-ADGroup "CorpSvcs_IOE_Infra&VA").DistinguishedName)'" -SearchBase $((Get-ADUser $User).DistinguishedName)) {$output+= "True "} Else {$output+= "False "}

                $output
}


Test-ADGroupMemberInfra -User "jimmy05" >> d:\grp1.txt
Avatar of ITguy565
ITguy565
Flag of United States of America image

You could use something like this :

https://stackoverflow.com/questions/23885149/get-recursive-group-membership-of-all-ad-users-using-powershell


Create the Following Function
Function Get-ADGroupsRecursive{
Param([String[]]$Groups)
    Begin{
        $Results = @()
    }
    Process{
        ForEach($Group in $Groups){
            $Results+=$Group
            ForEach($Object in (Get-ADGroupMember $Group|?{$_.objectClass -eq "Group"})){
                $Results += Get-ADGroupsRecursive $Object
            }
        }
    }
    End{
        $Results | Select -Unique
    }
}

Open in new window


import-module activedirectory

$users = get-aduser -Filter {Name -Like "*"} -Searchbase "ou=Users, dc=Domain" -Properties MemberOf | Where-Object { $_.Enabled -eq 'True' } 

$targetFile = "D:\users.csv"
rm $targetFile
Add-Content $targetFile "User;Group"


foreach ($user in $users)
{
    $Groups = $User.MemberOf
    $Groups += $Groups | %{Get-ADGroupsRecursive $_}
$Groups | %{New-Object PSObject -Property @{User=$User;Group=$_}}|Export-CSV $targetfile -notype -append
}

Open in new window



See if this is what you are looking for.
Avatar of Lucky Tham
Lucky Tham

ASKER

I'll try it and update you. Tks.
@Lucky,

Do you have an update for this question?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.