PowerShell Get-AdGroupMember is used to get members from the active directory. You can get ad group members by specifying the active directory group name. Identity parameter specifies the Active Directory Group to access to get members of the group.
As a System Administrator, we have to query the active directory to get list of users in AD group and export ad group members to CSV or any file format.
In this article, I will explain how to use the PowerShell Get-AdGroupMember cmdlet to get users from AD group, get members of ad group.
Get AD Group Members
Active Directory group can have multiple groups within it and have users associated with each group.
To get list of users from AD group and get groups from AD group, run the below command
Get-ADGroupMember -Identity "Shell_Sales" | Select-Object Name | Sort-Object Name
In the above PowerShell command, we have to get users and groups from the Shell_Sales ad group.
Using PowerShell Get-ADGroupMember cmdlet, it gets members from the active directory, members can be groups, users, or computers.
Using the Identity parameter, it returns users and groups from Shell_Sales active directory group.
In the next command, it selects the name of the object and sorts the object by Name before it lists all users in ad group to console.
The output of the above command to get ad group members as below
Name ---- Administrator ShellGeek Shell_Asia Shell_Europe
Get List of Users from Groups and Nested Groups
In the above example, we have discussed how to get users and groups from the active directory. As we know, active directory groups can have groups in them.
In the above example, the Shell_Sales ad group has Shell_Asia and Shell_Europe groups.
To get list of user from groups and nested groups, use the Get-ADGroupMember cmdlet to recursively check through specified by -Recursive parameter in active directory and get ad group members, run below command
Get-ADGroupMember -Identity "Shell_Sales" -Recursive | Get-ADUser -Property DisplayName | Select-Object DisplayName
In the above PowerShell script to get members of ad group,
Using PowerShell Get-AdGroupMemeber get members of the active directory group specified by the Identity parameter and passes ad group members to the second command.
The second command uses the Get-ADUser cmdlet to get ad users from a list of ad users and returns the display name
The third command, Select-Object displayname of all ad users from groups and nested groups on the console.
I hope the above article on how to get list of users from ad group and nested groups may help you to understand the use of the PowerShell Get-ADGroupMember cmdlet.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.