/commands/active-directory/get-adgroupmember-recursive
Get-ADGroupMember -Recursive: list all members of an AD group, including nested groups
- tool
- Active Directory PowerShell
- versions
- ActiveDirectory module (RSAT) on Windows 10/11 and Windows Server 2016+
- os
- windows
Get-ADGroupMember -Identity 'Sales' lists the direct members of a group: users, computers and other groups. Add -Recursive to expand nested groups and return only the leaf objects (users and computers), each listed once:
Import-Module ActiveDirectory
Get-ADGroupMember -Identity 'Sales' -Recursive |
Where-Object objectClass -eq 'user' |
Select-Object Name, SamAccountName |
Sort-Object Name-Identity accepts the group's name (sAMAccountName), distinguished name, GUID or SID.
Direct vs recursive
| Command | Returns |
|---|---|
| Get-ADGroupMember 'Sales' | direct members, including nested groups as group objects |
| Get-ADGroupMember 'Sales' -Recursive | users and computers from all nesting levels; no group objects |
| (Get-ADGroup 'Sales' -Properties Member).Member | distinguished names of direct members, read from the member attribute |
The objects returned are ADPrincipal objects with a few properties (Name, SamAccountName, objectClass, distinguishedName, SID). To get more user attributes, pipe them to Get-ADUser:
Get-ADGroupMember 'Sales' -Recursive |
Where-Object objectClass -eq 'user' |
Get-ADUser -Properties EmailAddress, Department |
Select-Object Name, EmailAddress, Department |
Export-Csv sales-members.csv -NoTypeInformationLarge groups
Get-ADGroupMember goes through Active Directory Web Services, which limits how many members it returns for one group (5,000 by default, the ADWS MaxGroupOrMemberEntries setting). Larger groups fail with a size-limit error. Alternatives that do not hit that limit:
# users that are members of the group, directly or through nesting (LDAP_MATCHING_RULE_IN_CHAIN)
$dn = (Get-ADGroup 'All Staff').DistinguishedName
Get-ADUser -LDAPFilter "(memberOf:1.2.840.113556.1.4.1941:=$dn)" | Select-Object Name, SamAccountName
# direct members only, from the member attribute
(Get-ADGroup 'All Staff' -Properties Member).Member.CountThe matching-rule OID 1.2.840.113556.1.4.1941 makes the domain controller walk the nested membership, which is also faster than -Recursive for deep nesting.
Which groups is a user in?
Get-ADPrincipalGroupMembership -Identity jdoe | Select-Object Name | Sort-Object NameThis lists direct group memberships only. Members through a user's primary group (usually Domain Users) are not stored in the member attribute, so attribute-based queries do not show them.
Requirements
The cmdlet belongs to the ActiveDirectory module, which is part of the Remote Server Administration Tools (RSAT) on Windows 10 and 11 and is installed with the AD DS tools on Windows Server; see install RSAT on Windows 11. It talks to Active Directory Web Services (ADWS) on a domain controller over TCP port 9389, not over plain LDAP. Reading group membership normally needs only a domain user account, because authenticated users can read the member attribute of most groups; protected groups and delegated OUs can be more restrictive.
Errors you may see
| Message | Cause |
|---|---|
| The term 'Get-ADGroupMember' is not recognized | The ActiveDirectory module (RSAT) is not installed or not imported |
| Unable to find a default server with Active Directory Web Services running | No reachable domain controller with ADWS; pass -Server or check port 9389 |
| Cannot find an object with identity | The group name, DN, GUID or SID does not exist in the domain you queried |
| Size limit error on a large group | More members than the ADWS limit; use the LDAP filter shown above |
Fixes are on Get-ADUser is not recognized (same module), Unable to find a default server and Cannot find an object with identity.
Pitfalls
- Display name versus account name.
-Identitydoes not search display names. If the group's display name differs from itssAMAccountName, look it up first withGet-ADGroup -Filter "Name -eq 'Sales Team'". - Primary group. Users whose primary group is the queried group are not listed through the
memberattribute, so attribute-based counts can be lower than what Active Directory Users and Computers shows.
FAQ
Why does -Recursive not show the nested groups themselves?
By design it returns only the leaf objects (users and computers) found at any depth. To see the group structure, run Get-ADGroupMember without -Recursive and follow the members whose objectClass is group.
How do I count the users in a group including nested groups?
Wrap the command and count the users, for example @(Get-ADGroupMember 'Sales' -Recursive | Where-Object objectClass -eq 'user').Count. For very large groups, count the result of the LDAP_MATCHING_RULE_IN_CHAIN filter instead.
How do I query a group in another domain of the forest?
Add -Server with a domain controller or the DNS name of that domain, for example -Server emea.contoso.com. Without it the cmdlet asks a domain controller of the computer's own domain.