Loop Through Local Administrators Group

cnbhold

New member
Joined
Nov 26, 2013
Messages
1
Programming Experience
1-3
I'm trying to create a C# console application that will loop through the local Administrators group and return only a list of users not groups. The following code returns both users and groups.
C#:
static void Main(string[] args)
        {            
            string hostName = "1234569";
            
            using (DirectoryEntry machine = new DirectoryEntry("WinNT://" + hostName))
            {                
                using (DirectoryEntry group = machine.Children.Find("Administrators", "Group"))
                {
                    object members = group.Invoke("Members", null);
                    foreach (object member in (IEnumerable)members)
                    {                        
                        string accountName = new DirectoryEntry(member).Name;
                        Console.WriteLine(accountName);
                    }
                }
            }

            Console.ReadLine();
        }
 
You could check DirectoryEntry.SchemaClassName
 
Back
Top Bottom