I would like to create a csv file that would contain all the members of a particular AD group

I need your help. I would like to create a csv file that would contain all the members of a particular AD group. In the csv file, there would be columns username, first and last name, and email address. Can you help me? Do you know of a GUI software that can do this?

Definitely software that can do it, but the ones I know are all paid solutions. (ManageEngine ADManager Plus is what we user.)

If you just want a CSV with that info, though, the cmdlets in the ActiveDirectory (ActiveDirectory Module | Microsoft Learn) module can do it. To install it, you have to add some optional features in Windows (How to install the PowerShell Active Directory module – 4sysops).

Get-ADGroupMember -Server <domain controller> -Credential <credentials with AD access> -Identity "<group name>" | Get-ADUser -Server <domain controller> -Credential <credentials with AD access> -Properties mail | Select-Object samaccountname, name, mail | Export-Csv -Path C:\temp\GroupMembers.csv

My particular setup requires the -Server and -Credential params for each AD cmdlet, but yours won’t if you’re on the same domain as your group and you’re running PowerShell as the user with the correct access.

Hi, masterofthehat! Thanks for your response and help. I will definitely try your tips.

I found a solution to my problem, although it is not a csv file but a txt file, but it works nicely. I don’t need -Server and -Credential because the group I’m interested in is on the same domain.

Get-ADGroupMember -Identity "<group name>" | Get-ADUser -Properties mail | Select-Object SamAccountName, GivenName, Surname, Mail | Format-Table @{e='SamAccountName';width=20}, @{e='GivenName';width=20}, @{e='Surname';width=40}, @{e='Mail';width=50} | Out-File -FilePath C:\GroupMembers.txt

Glad it works for you! Any reason you aren’t using Export-Csv?

1 Like

Hi,

The reason I don’t use Export-Csv is when I get the output I get everything in one column and for each user it’s all in one cell. My goal is to have 4 separate columns (username, first name, last name and email) and for each user I get information in the corresponding cell. I got this in the command I posted above, however it is a txt file and for many users txt is not very practical to copy and edit.

Get-ADGroupMember -Identity $GroupName | Get-ADUser -Properties mail | Select-Object SamAccountName, GivenName, Surname, Mail | Export-CSV -NoTypeInformation -Path .\test.csv

Works just fine for me (using Export-CSV). I typically save it as a CSV to save myself the trouble. It saves as 4 different columns. I strongly suggest not using out-file in this case and when exporting data don’t use format-table. Format-Table is really more for formatting output to the console, not in files. You can greatly simplify both your code, and its ability to be read by others.

2 Likes

Thank you very much for your help. I tried your code, however the same thing happens to me as I wrote above. When I get the output I get everything in one column and for each user it’s all in one cell. The results are not in 4 separate columns. Thanks again for your help, I’ll stick with the code that works for me for now until I find something better.

No worries, glad you have a solution that works for you!

1 Like