Forum Discussion

Diego13's avatar
Diego13
Copper Contributor
Nov 09, 2022

Get nested AD groups from bulk user or OU

Hi,

 

I would like to export in a csv file all groups and nested groups from bulk AD user or OU.

Example:

UsersGroupsNestedGroups         
User1Group1NestedGroup1NestedGroup2NestedGroup3       
 Group2NestedGroup1NestedGroup2        
 Group3NestedGroup1NestedGroup2NestedGroup3NestedGroup4NestedGroup5NestedGroup6NestedGroup7NestedGroup8NestedGroup9NestedGroup10
 Group4NestedGroup1NestedGroup2NestedGroup3NestedGroup4      
 Group5NestedGroup1NestedGroup2NestedGroup3NestedGroup4NestedGroup5     
User2Group1NestedGroup1NestedGroup2NestedGroup3       
 Group2NestedGroup1NestedGroup2        
 Group3NestedGroup1NestedGroup2NestedGroup3NestedGroup4NestedGroup5NestedGroup6NestedGroup7NestedGroup8NestedGroup9NestedGroup10
 Group4NestedGroup1NestedGroup2NestedGroup3NestedGroup4      
 Group5NestedGroup1NestedGroup2NestedGroup3NestedGroup4NestedGroup5     

 

Thanks 

        

  • Alan2022's avatar
    Alan2022
    Iron Contributor

    Diego13 

     

     

    # Filter Groups in AD based on OU
    $searchBase = "OU=Test,DC=company,DC=com"
    Get-ADGroup -Filter * -SearchBase $searchBase -Property * | Sort-Object Name 
    
    # Get List of Members base on Group Name
    Get-ADGroupMember -Identity '<GroupName>' -Recursive | Select-Object -Property @{n="Members"; e={ $_.Name }} | Sort-Object Members

     

    • Diego13's avatar
      Diego13
      Copper Contributor
      Hi Alan, thanks for the powershell provided.
      Is it possible to export the results as requested please ?
      • Alan2022's avatar
        Alan2022
        Iron Contributor

        Diego13 

        Hi,

         

        Yes you can just save them in a variable like $result. Then use export to csv ps script.

        $result | Export-Csv c:\temp\test.csv -NoTypeInformation

         

Resources