Share via


Searching for Groups

This topic shows how to search for groups using DirectorySearcher.

The following C# example shows how to search for all groups on a domain.

using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
foreach(SearchResult res in src.FindAll())
{
    Console.WriteLine(res.Path);
}

The following C# example shows how to search for all security-enabled groups. For this search, use COM InteropUsing COM Interop to Access ADSI. For more information, see the topic "COM Interoperability in Visual Basic and Visual C#" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252. This example uses a bitwise search.

using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
int val = (int) ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED;
string query = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
    Console.WriteLine(res.Path);
}

The following C# example shows how to search for all global domain groups, regardless of whether they are secure or non-secure. For this search, use COM InteropUsing COM Interop to Access ADSI. For more information, see the topic "COM Interoperability in Visual Basic and Visual C#" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
int val = (int) ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP;
string query = "(&(objectCategory=group)(groupType:1.2.840.113556.1.4.804:=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
    Console.WriteLine(res.Path);
}

The following C# example shows how to search for all global domain secure groups. For this search, use COM InteropUsing COM Interop to Access ADSI. For more information, see the topic "COM Interoperability in Visual Basic and Visual C#" in the MSDN Library at https://go.microsoft.com/fwlink/?LinkID=27252.

[C#]

using System.DirectoryServices;
...
DirectorySearcher src = new DirectorySearcher(ou,"(objectCategory=group)");
int val = (int) (ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_GLOBAL_GROUP 
    | ActiveDs.ADS_GROUP_TYPE_ENUM.ADS_GROUP_TYPE_SECURITY_ENABLED);
string query = "(&(objectCategory=group)(groupType=" + val.ToString() + "))";
src.Filter = query;
foreach(SearchResult res in src.FindAll())
{
    Console.WriteLine(res.Path);
}

See Also

Reference

DirectorySearcher
System.DirectoryServices

Concepts

Group Management

Send comments about this topic to Microsoft.

Copyright © 2007 by Microsoft Corporation. All rights reserved.