0
votes

I'm trying to export group members and their properties from groups with names starting with XX so the output is

output

With the code i wrote i can get all the users and properties but i would also like to export the group in front of each member

$Groups = Get-ADGroup -filter {name -like "Group*"} | Select-Object Name
foreach ($Group in $Groups)
    {
    $Members = Get-ADGroupMember -Identity $($Group.name) -Recursive | Select-Object name
    foreach ($Member in $Members )

        { Get-ADUser -Identity $($Member.name) -Properties * | Select-object * | export-csv 

        }
    }

Thank you in advance, guys.

1

1 Answers

0
votes

Store the user object in a variable, add the group as a new member of that object and pipe it to Export-CSV.

$Groups = Get-ADGroup -filter {name -like "Group*"} | Select-Object Name
foreach ($Group in $Groups) {
    $Members = Get-ADGroupMember -Identity $($Group.name) -Recursive | Select-Object name
    foreach ($Member in $Members) {
        $UserObject = Get-ADUser -Identity $($Member.name) -Properties * | Select-Object *
        $UserObject.Group = $($Group.name)
        $UserObject | Export-CSV
    }
}