How do I find the total number of servers in each Organizational Unit?

When working in an unfamiliar Active Directory environment with little or no documentation, knowing where things are (or where they are more likely to be) can help you get started a lot more quickly. And one question I always have is “where do you store your server objects in AD?”

When approaching this problem, I came up with Test 1 (below) as my initial solution – querying for all servers, and then grouping by OU.

Dr Google seems to suggest that the rest of the Internet prefers the approach in Test 2; where you find all OUs, then interrogate each OU for any servers.

So, here are both versions.  I added the capturing of timestamps to calculate which one ran faster.  So if you use either of the below scripts, you can strip out the time-related bits.

 

 

 

In my case, Test 1 proved to be the faster solution to execute:

 

And don’t forget that the actual results you are going to use are stored in the $resulttable1/$resulttable2 variables.

 

Leave a Reply

Your email address will not be published. Required fields are marked *