PowerShell Script To Get Active Directory Group Members

Photo of author

By Victor Ashiedu

Published

This guide is a hands-on step by step showing how to write a Powershell script to Get AD Group Members. The final script is included at the end of the guide.

What You Need to Write a Powershell Script to Get AD Group Members

To build this script you will need the following:

  1. Basic knowledge of PowerShell commands
  2. A PowerShell script Editor (PowerShell ISE or PowerGUI)
  3. The name of the AD group you want to export its members

Powershell Script to Get AD Group Members (Basic Commands)

In this demo, I will export members of the group shown in the image below:

Follow the steps below to build the Powershell Script to Get AD Group Members:

  • Open your script editor. I will be using PowerGUI. Then open a new scripting window. Save the file as Export-ADGroupMembers
powershell get ad group members

The first step is to determine the PowerShell cmdlets we need. The easiest way to find a PowerShell command is to use the Get-Command cmdlet.

The beauty of Powershell is that the commands are descriptive. So to find a command that exports (gets AD members), run the command below:

Get-Command -Name *GroupMember

Here is the result of the command:

The command we are looking for is Get-ADGroupMember

  • The next step is to determine how to use this command. To do this we will run the command below:
Get-Help Get-ADGroupMember

Here is the result

Looking at the syntax, I can see a parameter called [-Identity] <ADGroup>. It means that if I know the name of the group I can use the command below to lists its members:

Get-ADGroupMember -Identity "Windows Admins"
In the last command, “Windows Admins” is the name of the AD group

The result in PowerShell:

powershell get ad group members - Get-ADGroupMember -Identity "Windows Admins"

You would have thought that the result will give us exactly what we need. Unfortunately, it returned more than one property of an AD User. It requires more work.

Before we proceed, we will update our script. Copy the command Get-ADGroupMember -Identity “Windows Admins” into your script editor.

Remember to change the AD group name to the actual name of the group in your environment

The updated Powershell script to get AD Group members is shown below:

powershell get ad group members - Updated script
  • Next step is to add the command to a variable, I will call the variable $Members. The updated PowerShell script to get AD group members will now look like this:

The next section takes the script to the next level!

Powershell Script to Get AD Group Members (Advanced)

In this section I will build on what I have done so far. Here is what we have so far.

Look at the result closely. Each user has the following properties: distinguishedName, name, objectClass, objectGUID, SamAccountName and SID. But I just need the users name and Login Name (SamAccountName).

I will modify the script to display only these two properties. Here is the updated PowerShell script to get AD group members script.

The only new addition is Select-Object command. I simply piped the last command (stored in the $Members) into Select-Object so I can return just the two properties I require. Here is the result in PowerShell.

This looks good but we can make the output look more user friendly. I will like the script to replace “name” with “Full Name” and “SamAccountName” with “User Name”.

Here is the updated Powershell script to get AD group members:

It is exactly the same script with the Select-Object command modified. The result of the script so far is shown below
powershell get ad group members - output of the script so far.

The output is looking just the way I want it! In the next section I will add one last bit to the script.

Powershell Script to Get AD Group Members (Export Result to CSV)

To complete the script I want to add the last bit: a command that will export the output to CSV. I will simply pip the output into the Export-CSV command.

Here is the final script

Powershell Get AD Group Members Script (Export Result to CSV)

And the CSV output

Powershell Get AD Group Members Script (Export Result to CSV)

You can copy the script below:

$Members = Get-ADGroupMember -Identity "Windows Admins"
$Members | Select-Object @{Name="Full Name";Expression={$_.name}}, `@{Name="User Name";Expression={$_.SamAccountName}} | `
Export-CSV -NoTypeInformation -Path C:\PS\Export-ADGroupMembers\CSV-Report.csv

Hope you see how easy it is to write a PowerShell to get ad group members. The aim of the guide is to walk you through how I write a typical PowerShell script.

I believe it benefits you more to learn how to write a PowerShell script instead of just copying a script.

I hope you found this script helpful. If you did, kindly spare 2 minutes to share your experience with our community at [discourse_topic_url].

However, if you have any questions regarding this article or if the steps did not fix your problem, please post your question at [discourse_topic_url]. Our team and other community members will come back to you with a fix as soon as possible.

Lastly, to learn more about PowerShell, visit our PowerShell Page.

Other Helpful Guides

Additional Resources and References

We go the extra mile to deliver the highest quality content for our readers. Read our Content Writing, Content Review, and Anti-Plagiarism policies to learn more.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

If this article does not meet your expectations, kindly let us know. We have various ways you can get in touch with us:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment

Send this to a friend