question

JakubGr avatar image
0 Votes"
JakubGr asked JakubGr answered

[Active Directory] Export group members to CSV with their emails

Hello,

I work in a larger globally managed company. I need to export a list of users from a particular group to a CSV file. I am using a simple command in PowerShell:

"Get-AdGroupMember -identity "group-name" | select name | Export-csv -path C:\members.csv -NoTypeInformation"

This is where the problem starts because I need to list people by their first and last name and in PowerShell after a command:

"Get-AdGroupMember -identity "group-name""

is called these people are listed by their ID number (below is an example)

name : 00022001
object class : user
SamAccountName : 00022001

In Active Directory Users and Computers, the example above (that is, person 0022001) is shown like this:

0022001

first name: Jan
Last name: Kowalski
Display name: Jan Kowalski
Discription:
Office: xxx
Email: jankowalski@xxx.com


My question is: How to export a list of users from a group including their email addresses or first and last name instead of an ID number?


I am a beginner so please give me clear guidance on the problem.
Thank you for your help.

Regards,
Jakub.





windows-server-powershellwindows-active-directory
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NewbieJones-6218 avatar image
1 Vote"
NewbieJones-6218 answered

The default attributes that Get-ADGroupMember returns are fairly basic.
distinguishedName, name, objectClass, objectGUID, SamAccountName and SID.

If you want more detailed user information, you will need to pipe the result into Get-ADUser.

Get-ADUser itself only shows a few attributes by default.
However GivenName and SurName are included in the default attributes.
If you want other attributes, you will need to include them by using -properties.

You can then pipe the results of this into Select-Object to control which attributes you want returned, dropping any extraneous ones.

 Get-ADGroupMember -identity $group | 
     Get-ADUser | 
         Select-Object GivenName, SurName |
             Export-CSV c:\members.csv -NoTypeInformation
    
 # version 2 - how to return non default attributes like displayName and mail.
    
 Get-ADGroupMember -identity $group | 
     Get-ADUser -properties displayName, mail | 
         Select-Object displayName, mail |
             Export-CSV c:\members.csv -NoTypeInformation
    
 # version 3, want the group name included in that
    
 Get-ADGroupMember -identity $group | 
     Get-ADUser | 
         Select-Object GivenName, SurName, @{name="group";expression={$group}} |
             Export-CSV c:\members.csv -NoTypeInformation



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JakubGr avatar image
0 Votes"
JakubGr answered

@NewbieJones-6218

Your answer proved correct, thank you for your quick and complete instruction.

I wish you the best of luck.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.