Export group members RecursiveMatch

skip hofmann 1 Reputation point
2022-11-08T19:50:11.277+00:00

Hello all

I came across this script . The script is looking at the parent group and exporting all members to a unique .csv file, and its excluding groups. This is very close to what i need, except i want the export to one .csv file and do not exclude groups.
Any help is greatly appreciated

 $Input_Users = "Name,Description,UserprincipalName,SamAccountName,office,DepartmentNumber,Department" -split ','  
 $GroupNames = @()  
      
 # get the users in the main group  
 Get-ADGroupMember -Identity "GP1" |  
     ForEach-Object {   
         if ($_.objectClass -eq "user") {   
             Get-ADUser $_.SamAccountName -Properties $Input_Users | Select-Object $Input_Users   
         }   
         elseif ($_.objectClass -eq "Group") {   
             $GroupNames += $_.distinguishedName  
         }   
     } | Export-Csv -Path C:\temp\ParentGroupUsers.csv -NoTypeInformation  
 # get the users (ignoring other ogject types) that are members of each child group  
 # place the users in a unique CSV for each group  
 ForEach ($group in GroupNames){  
     $g = "c:\temp\{0}.csv" -f (Get-ADGroup -Identity $group).Name  
     Get-ADGroupMember -Identity $group |  
         ForEach-Object{  
             Get-ADUser $_.SamAccountName | Select-Object $Input_Users  
         } | Export-Csv $g -NoTypeInformation  
 }  
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,432 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,416 Reputation points
    2022-11-08T20:04:34.217+00:00

    Did you look at your previous question (export-ad-group-membership.html) before posting this one?