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).

category: .NET, Networking    

1 Comment so far

  1.    Robert on January 25th, 2008

    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)