GroupPrincipal.GetMembers Method (Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Returns a collection of the principal objects that is contained in the group. When the recursive flag is set to true, this method searches the current group recursively and returns all nested group members.

Namespace:   System.DirectoryServices.AccountManagement
Assembly:  System.DirectoryServices.AccountManagement (in System.DirectoryServices.AccountManagement.dll)

public PrincipalSearchResult<Principal> GetMembers(
	bool recursive


Type: System.Boolean

A Boolean value that specifies whether the group is searched recursively.

Return Value

Type: System.DirectoryServices.AccountManagement.PrincipalSearchResult<Principal>

A T:System.DirectoryServices.AccountManagement.PrincipalSearchResult'1 object that contains the principal objects that are members of the group, or an empty collection if the group has no members.

The returned principal collection does not contain group objects when the recursive flag is set to true; only leaf nodes are returned. For example, when a group that contains a computer object and a group object (with only user principals) is searched recursively, the returned collection contains the computer object and the user principal objects in the nested group. Since the group object is not a leaf, even when it is empty, it is not returned in the recursive search. When the recursive flag is set to false, the returned collection may contain group objects.

Members are returned without respect to the context. For example, if an AD DS context based at "CN=SpecialUsers,DC=Fabrikam,DC=com", the PrincipalFindResult set will include group members that are located under "CN=NormalUsers,DC=Fabrikam,DC=com" also, even though they fall under a scope that is not part of the context that is searched. The returned principal collection may also contain members that are located in a different store than the group.

The following code connects to the LDAP domain "" with the username set to "administrator" and the password set to "SecretPwd123" in the PrincipalContext constructor.

A search is performed to find the group that has the name "Domain Admins" under the container specified in the PrincipalContext constructor "DC=fabrikam,DC=com." If the group is found, all the principals that are members of this group, which includes recursive members, are then enumerated

PrincipalContext ctx = new PrincipalContext(ContextType.Domain,                                                                    

GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, 
                                                   "Domain Admins");

if (grp != null)
    foreach (Principal p in grp.GetMembers(true))


.NET Framework
Available since 3.5

GetMembers Overload
GroupPrincipal Class
System.DirectoryServices.AccountManagement Namespace Overview

Return to top