0

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 
TylerH
  • 20,799
  • 66
  • 75
  • 101
Sen
  • 3
  • 3

1 Answers1

0

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 
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Each username in the group and each group description repeat 3 times in the exported csv file otherwise it is exactly what I need. SamAccountName, GroupName , Description User1, User2, User1, User2, User1, User2, test-group , K:\test, K:\test, K:\test, K:\test, K:\test, K:\test – Sen Apr 23 '22 at 13:27
  • @Sen Ah, yes I see. Please see my edit – Theo Apr 23 '22 at 13:50
  • Group.txt in my case contain most of the time duplicate group names, when this is the case, the code above ignores the duplicates in group.txt, it seems, and the csv contains only the unique groups from groups.txt. I did not see this before, is there a way to solve this? I need the code to read each group even if duplicate and put this in the csv file. – Sen May 16 '22 at 13:40
  • @Sen Please see the edit – Theo May 16 '22 at 14:39