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:
- Basic knowledge of PowerShell commands
- A PowerShell script Editor (PowerShell ISE or PowerGUI)
- 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
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"
The result in PowerShell:
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.
The updated Powershell script to get AD Group members is shown below:
- 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.
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:
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
And the CSV output
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
- PowerShell Array and Add to Array: Applications and Examples
- Powershell NoTypeInformation: Applications and Examples