Skip to content

List all computers in active directory

November 18, 2014

If you have a very big domain, or have setup limits on your domain how how many items can be returned pr search you might have to use paging.

public static List<string> GetComputers()
    List<string> ComputerNames = new List<string>();

    DirectoryEntry entry = new DirectoryEntry("LDAP://");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = ("(objectClass=computer)");
    mySearcher.SizeLimit = int.MaxValue;
    mySearcher.PageSize = int.MaxValue;

    foreach(SearchResult resEnt in mySearcher.FindAll())
    	string ComputerName = resEnt.GetDirectoryEntry().Name;
    	if (ComputerName.StartsWith("CN="))
    		ComputerName = ComputerName.Remove(0,"CN=".Length);


        return ComputerNames;

--- -- ---- ------------------------------------------------------------------ ------ - -- -                          -- -             ---------------------


From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: