Author Posts

July 19, 2018 at 11:39 pm

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

July 20, 2018 at 12:17 am

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.

July 20, 2018 at 5:33 pm

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.

July 23, 2018 at 5:32 pm

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

July 23, 2018 at 5:34 pm

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