Im wondering how to get a list of all computers / machines / pc from active directory?
(Trying to make this page a search engine bait, will reply myself. If someone has a better reply il accept that )
Im wondering how to get a list of all computers / machines / pc from active directory?
(Trying to make this page a search engine bait, will reply myself. If someone has a better reply il accept that )
If you have a very big domain, or your domain has limits configured on how how many items can be returned per search, you might have to use paging.
An LDAP query like:
(objectCategory=computer)
should do the trick.What EKS suggested is correct, but is performing a little bit slow.
The reason for that is the call to
GetDirectoryEntry()
on each result. This creates aDirectoryEntry
object, which is only needed if you need to modify the active directory (AD) object. It's OK if your query would return a single object, but when listing all object in AD, this greatly degrades performance.If you only need to query AD, its better to just use the
Properties
collection of the result object. This will improve performance of the code several times.This is explained in documentation for
SearchResult
class:Here is an example on how to use the
Properties
collection:Documentation for
SearchResult.Properties
Note that properties can have multiple values, that is why we use
Properties["name"].Count
to check the number of values.To improve things even further, use the
PropertiesToLoad
collection to let the searcher know what properties you are going to use in advance. This allows the searcher to only read the data that is actually going to be used.