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

Jakub Grochowski 21 Reputation points
2022-05-05T09:07:24.007+00:00

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@X .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.

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,898 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Newbie Jones 1,306 Reputation points
    2022-05-05T11:20:43.713+00:00

    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  
    
    
    
      
      
      
    
    8 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jakub Grochowski 21 Reputation points
    2022-05-05T11:44:26.617+00:00

    @Newbie Jones

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

    I wish you the best of luck.

    0 comments No comments