Share via


Searching for Groups

This topic shows how to search for groups using DirectorySearcher.

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

[C#]

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

The following code example shows how to search for all security enabled groups. For this search, use COM Interop. It uses a bitwise search.

[C#]

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 code example shows how to search for all global domain groups, regardless of whether they are secure or non-secure. For this search, use COM Interop.

[C#]

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 code example shows how to search for all global domain, secure groups. For this search, use COM Interop.

[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);
}