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.

 

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

  1. Alex

    get-adcomputer : The term ‘get-adcomputer’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, v
    erify that the path is correct and try again.

    How to recognize the command?
    Thanks

    Reply
    1. Kamal

      You need to use this, first:

      import-module activedirectory

      That assumes you’re on a Domain Controller, or have the AD Powershell cmdlets installed where you’re running the script from.

      Reply

Leave a Reply

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