4
votes

If I run 'net group /domain' on the Windows command line, I get a list of "global groups" for the current domain server. I want to get the same list from my .NET code.

Googling around I find thousands of "How to get the groups a user belongs to", "How to get the users that belong to a group", etc. I just want the list of all global groups; not those associated with a particular user. Can someone show me the code for this, or reference from which that code could be derived?

1
See the System.DirectoryServices namespaceL.B

1 Answers

4
votes

This code will find all Global groups in the current domain:

using (var context = new PrincipalContext(ContextType.Domain))
using (var filter = new GroupPrincipal(context) { GroupScope = GroupScope.Global })
using (var searcher = new PrincipalSearcher(filter))
{
    var groups = searcher.FindAll();
}