Enumerating Computers in a Domain

I saw a question in a forum asking how to enumerate computers available in a domain. Though it might seem little difficult, it can be accomplished in less than 10 line of code. Thought would make it as a post for everyone’s benefit. Here we go:


public void PrintComputersInDomain (string domainName)
    DirectoryEntry de = new DirectoryEntry ("LDAP://" + domainName);
    de.Children.SchemaFilter.Add ("computer");
    foreach (DirectoryEntry c in de.Children)
        Console.WriteLine (c.Name);

Please note when you try this code, it might take seconds to finish if there are hundreds of computers in the given domain (of course, you can always do “kill process” J).

One thought on “Enumerating Computers in a Domain”

  1. Cool – thanks! Got any scripts that could help identify stale Computer object in by AD? (Comptuer that are no longer actively runing in my domain)

Comments are closed.