Export members of multiple groups

2.3k views Asked by At

Is it possible to change the PS script below in two ways:

  1. The group members are now exported horizontally but I want all the users in 1 cell in the column beside the group name. We have a lot of groups and it is not readable this way.
  2. The path to the folders in the description field of the AD groups are not exported. I would like to have the content of the description field also exported in the column beside the group.

I would like to see this result, see the photo below please:

enter image description here

cls
$Groups = "Group1", "Group2", "Group3"
$results = foreach ($Group in $Groups) {
  Get-ADGroupMember -Server contoso.com $group | 
    select SamAccountName, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
}
$results
$results | Export-csv C:\TEMP\GroupMemberShip.CSV -NoTypeInformation 
1

There are 1 answers

4
Theo On BEST ANSWER

With some minor changes to your original code, you could first gather the wanted info per group and before exporting to CSV, use Group-Object to merge the details.

Something like:

$Groups = "Group1", "Group2", "Group3"
$results = foreach ($Group in $Groups) {
    $adGroup = Get-ADGroup $group -Properties Description -ErrorAction SilentlyContinue
    if ($adGroup) {
        $adGroup | Get-ADGroupMember -Server 'contoso.com' | 
        Select-Object SamAccountName, 
                      @{Name = 'GroupName'; Expression = {$adGroup.Name}}, 
                      @{Name = 'Description'; Expression = {$adGroup.Description}}
    }
    else {
        Write-Warning "Group '$group' could not be found.."
    }
}

# now group the results on the GroupName property and 
# return objects with joined SamAccountNames and Descriptions
$results | Group-Object GroupName | ForEach-Object {
    [PsCustomObject]@{
        SamAccountName = ($_.Group.SamAccountName | Sort-Object -Unique) -join ', ' 
        GroupName = $_.Name
        Description = ($_.Group.Description | Sort-Object -Unique) -join ', ' 
    }
} | Export-Csv -Path 'C:\TEMP\GroupMemberShip.CSV' -NoTypeInformation 

Although I don't understand why you would like to have duplicate items in your output, you can do this like below

$Groups = "Group1", "Group2", "Group3", "Group2", "Group3"
$results = foreach ($Group in $Groups) {
    $adGroup = Get-ADGroup $group -Properties Description -ErrorAction SilentlyContinue
    if ($adGroup) {
        $adGroup | Get-ADGroupMember -Server 'contoso.com' | 
        Select-Object @{Name = 'SamAccountName'; Expression = {($_.SamAccountName | Sort-Object -Unique) -join ', '}},
                      @{Name = 'GroupName'; Expression = {$adGroup.Name}}, 
                      @{Name = 'Description'; Expression = {$adGroup.Description}} -ExcludeProperty SamAccountName
    }
    else {
        Write-Warning "Group '$group' could not be found.."
    }
}

$results | Sort-Object GroupName | Export-Csv -Path 'C:\TEMP\GroupMemberShip.CSV' -NoTypeInformation