
First, you need to install the NuGet Package System.DirectoryServices.AccountManagement
Now you can open a context and get all members of the built-in group “Administrators”
1// This works only on Windows!
2using System.DirectoryServices.AccountManagement;
3using (PrincipalContext ctx = new PrincipalContext(ContextType.Machine))
4using (GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "Administrators"))
5{
6 if (grp is not null)
7 {
8 Console.WriteLine("Output:");
9 foreach (Principal p in grp.GetMembers(recursive: true))
10 {
11 Console.WriteLine(p.Name);
12 }
13 }
14}
Result:
1Output:
2Ben
3Administrator

Comments