Use PowerShell to Search AD DS and Produce an Uptime Report

Doctor Scripto

Summary: Learn how to use Windows PowerShell to Search Active Directory Domain Services for servers and produce an uptime report.

Hey, Scripting Guy! Question Hey, Scripting Guy! I enjoyed your series of blogs last week, and I consider uptime reports to be a vital topic. In fact, this was the very task I was assigned to do in my new job—write a script to obtain an uptime report and a free disk space report. I tried in VBScript and it was too complicated. Eventually, I hit upon your blog. The problem is that we have a large domain, and creating a list of servers is not practical. I looked at using other tools to query Active Directory, but they bring back too much garbage. I need an easier solution. Can you help?


Hey, Scripting Guy! Answer Hello BB,

Microsoft Scripting Guy, Ed Wilson, is here. There are many ways to searchActive Directory Domain Services (AD DS) from within Windows PowerShell. The techniques range from downloading and installing non-Microsoft tools, to installing the admin tools and quering a Windows Server 2008 R2 domain controller, to installing the Active Directory Management Service, to another technique. In fact, there are at least four ways to query AD DS without installing anything. Today I am going to look at using the ADSISearcher to search Active Directory and return only servers. I will then use this technique with the uptime report from last week.

Using the ADSISearcher

To bring back a listing of all computers in Active Directory, I use a command similar to the one here.


The command can be broken into several pieces, but I often type it as a single command for simplicities sake. The command is three parts:

The adsi searcher:


The filter:


The command to find all matches:


The object returning from the findall method is a DirectorySearches.SearchResult object. It contains two properties: the path and the properties properties. An example of the command and the associated output are shown in the image that follows.

Image of command output

To find the names of the computers, I use the path property with the [adsi] type accelerator, and I retrieve the cn property. An example of such a command is shown here.

([adsisearcher]”objectcategory=computer”).findall() | ForEach {([adsi]$_.path).cn}

To find the operating system, I use the OperatingSystem property from Active Directory. I can see the OperatingSystem information by using a command such as the one shown here.

([adsisearcher]”objectcategory=computer”).findall() | ForEach {([adsi]$_.path).operatingsystem}

The results from the previous command appear in the following image.

Image of command output

All of the Server operating systems contain the word server in the name. Therefore, I can use a compound LDAP query and filter out computers that contain the name server in the name. The command is shown here.


To retrieve only the server names from the previous command, I pipe the results to the ForEach-Object cmdlet, use the [adsi] type accelerator, and select the cn property. This command is shown here.

([adsisearcher]”(&(objectcategory=computer)(OperatingSystem=*server*))”).findall() | 

Foreach-Object {([adsi]$_.path).cn}

I store the retrieved servers in an array as shown here.

[array]$servers = ([adsisearcher]”(&(objectcategory=computer)(OperatingSystem=*server*))”).findall() |

      foreach-object {([adsi]$_.path).cn}

When I run the script, the report in the image that follows appears.

Image of command output

I uploaded the complete script to the Scripting Guys Script Repository. You can refer to that script for more details.

 BB, that is all there is to using Windows PowerShell to query Active Directory and to produce an uptime and disk free space report. Join me tomorrow for more Windows PowerShell cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Discussion is closed.

Feedback usabilla icon