Get members list from multiple AD groups

Hello I have this code:

$groups = "Group1", "Group2", "Group3","Group4"
$results = foreach ($group in $groups) {
    Get-ADGroupMember $group | select samaccountname, name, mail, @{n='GroupName';e={$group}}, @{n='Description';e={(Get-ADGroup $group -Properties description).description}}
$results | Export-csv C:\GroupMemberShip.csv -NoTypeInformation

and I wanted to get also email address, but the above code doesn’t return this value. Any ideas what to change in case to get also email address?

Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

You need to query each individual user account the same way you determine the group description with a calculated property.

BUT: Depending on the groups you’re processing there could be a lot of redundancy when you query a lot users who are members of multiple groups at the same time.

Hello Olaf,
Thank you for that, I’ve edited the post to have the correct format (sorry for that).
So you think I should do it same way as for the group description something like that:
Get-ADGroupMember $group | for each {$user = get-aduser $_ -properties mail etc

It depends on the AD structure and on the amount of users and groups you have but - yes - that’s what I meant. :wink: