Export members of multiple groups into single file

Hi,

I am trying to get a list of group members for multiple groups from active directory and export the list into csv file. I am getting names and SamAccountName with my script but I need users Email Addresses as well. If anyone can help me out that would be great. here is my script:

Import-Module ActiveDirectory
$groups = Get-Content C:\Groups.txt
$resultsarray =@()
foreach ($group in $groups) {
$resultsarray += Get-ADGroupMember -Id $group | select Name,SamAccountName,@{Expression={$group};Label=“Group Name”}
}

$resultsarray| Export-csv -path C:\AllADUsers.csv -notypeinformation

Quick and dirty, add this to your “select” section

@{l="EmailAddress";e={(Get-ADUser $_ -Properties emailaddress).emailaddress}}

“l” and “e” are just shortened versions of “Label” and “Expression”
Get-ADUser $_ will get each user in the pipeline as they stream out from from your Get-ADGroupMember command. Then it’s just a matter of selecting your desired properties, emailaddress in this instance.

Thank you. Worked as expected. I am also trying to export each group separately into multiple sheets in one excel. Is that practically possible? If so can you please help me.

I am seeking help to export each Active Directory groups and group members separately into multiple sheets in one excel.

You could make your life easier when you use the Powershell module ImportExcel for that.