Modify the output , 1 Adgroup as a header

Hello,

I have the uploaded script to get the memebers of Mutliple ADgroups.

In this , the output gives me per ADgroup, the members.

Groupa usernamea upna
Groupa usernameb upnb
Groupa usernamec upnc

In what way can I edit this,so that the output wil look like

ADgroupnameA
usera
userb
userc

ADgroupnameB
userx
usery
userz

And to create a output that shows what user is member of multiple adgroups.

$Groups = @(
        "groupa"
        "groupb"
        "groupc"
        "groupd"
                      )

$Table = @()

$Result = [ordered]@{
"Group Name" = ""
"Name" = ""
"Username" = ""
}


Foreach ($Group in $Groups)
{
    $Members = Get-ADGroup $Group -Properties members | select name,members

    foreach ($Member in $Members.members)
    {
   
        $UserMember = Get-ADuser $Member
        $Result."Group Name" = $Group
        $Result."UserName"   = $UserMember.samaccountname
        $Result."Useraccount"= $UserMember.UserPrincipalName
        $objRecord = New-Object PSObject -property $Result
        $Table += $objrecord

    }

}

$Table | export-csv "c:temp\filename.csv" -NoTypeInformation

thx

Before we dive into that rabbit hole … may I ask whatfor do you need the output in this way? … I’m asking because this way the data would be unusable for any other purpose than output in the console. :man_shrugging:t3:

If I got this right you should be able to achieve that with grouping the result with

"Before we dive into that rabbit hole … may I ask whatfor do you need the output in this way? … I’m asking because this way the data would be unusable for any other purpose than output in the console.

The reason for this is because of a audit where the client need to prove that there is a structured overview of rights to use a application.
In this case we use the AD groups that has the rights to the application/ data and extract the members.

I actually did not get why this way should be better for that purpose but anyway …

$GroupAndMemberList = @'
GroupName,UserName,UserPrincipalName
GroupA,usernamea,upna
GroupA,usernameb,upnb
GroupA,usernamec,upnc
GroupB,usernameb,upnb
Groupa,usernamec,upnc
GroupC,usernamea,upna
GroupC,usernameb,upnb
'@ |
ConvertFrom-Csv 

$GroupAndMemberList | 
Group-Object -Property GroupName |
ForEach-Object {
    $_.Name | Out-Host
    $MemberList = 
    foreach ($Member in $_.Group) {
        [PSCustomObject]@{
            UserName          = $Member.UserName
            UserPrincipalName = $Member.UserPrincipalName
        }
    }
    $MemberList | Out-Host
}

But again … You cannot use this output to process it any further with PowerShell. It’s meant to be seen on screen in the console.

The result would look like this:

GroupA

UserName  UserPrincipalName
--------  -----------------
usernamea upna
usernameb upnb
usernamec upnc
usernamec upnc


GroupB

UserName  UserPrincipalName
--------  -----------------
usernameb upnb


GroupC

UserName  UserPrincipalName
--------  -----------------
usernamea upna
usernameb upnb

I’m gonna borrow a little bit of code from Olaf. It can also be simplified like this too:

$GroupAndMemberList = @'
GroupName,UserName,UserPrincipalName
GroupA,usernamea,upna
GroupA,usernameb,upnb
GroupA,usernamec,upnc
GroupB,usernameb,upnb
Groupa,usernamec,upnc
GroupC,usernamea,upna
GroupC,usernameb,upnb
'@ |
ConvertFrom-Csv 

$GroupAndMemberList | 
Group-Object -Property GroupName | ForEach-Object {
    $_.Group.Username | ConvertFrom-Csv -Header $_.Name | Format-Table
}