[ACCEPTED]-Get Groups From OU using DirectoryServices.AccountManagement-directoryservices

Accepted answer
Score: 42

You can set the PrincipalContext to the OU where you want 2 to start the search and use the PrincipalSearcher-class in 1 System.DirectoryService.AccountManagement to accomplish what you need, like this:

PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.local", "OU=Marketing,OU=Operations,OU=Applications,DC=mycompany,DC=local");
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach(var group in ps.FindAll())
{
  Console.WriteLine(group.DistinguishedName);
}
Console.ReadLine();

More Related questions