Ping Active Directory servers with PowerShell

Posted On 2016-12-29 by dwirch
Tags: Powershell Scripting Tutorial Windows
Views: 777

Using the ping command to test for responses from network computers is a quick way to see if a network path is open between two hosts. Not in depth, but basic. Here is how to get a list of servers from Active Directory, and ping them with PowerShell.

This is another basic script to show how to grab a list of computers from Active Directory from a specific organizational unit (OU), store them in a list, then ping them. I've been asked for this knowledge more than once, so now it has become a post that I can point people at.

The first thing the script does is load the Active Directory module. This module makes getting a list of machines from AD much easier.

Next, we use the Get-ADComputer cmdlet to grab a list of computer information, specifically from the Servers OU. This is specified in the SearchBase parameter, by using the distinguished name of the OU. The list is stored in the variable $HostList.

Now that the list is acquired, a simple ForEach loop is used to roll through the list. Each entry is checked with the Test-Connection cmdlet. If the remote host responds, a "responding" message is written to the screen. If not, then a "not responding" message is written.

Here is the script:

import-module ActiveDirectory

$HostList=Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=labps,DC=local" -SearchScope Subtree

ForEach($Server in $HostList)
    if(test-connection -computername $HostToPing -count 1 -quiet )
      write-host "$HostToPing is responding"
      write-host "$HostToPing is not responding"

And a sample output:

server01.labps.local is not responding
server02.labps.local is responding
server03.labps.local is responding
server04.labps.local is responding
server05.labps.local is responding
server06.labps.local is responding
server07.labps.local is responding
server08.labps.local is responding
server09.labps.local is responding

This is just a basic example, and could be expanded on quite a bit. By expanding just a bit, you could potentially have a basic uptime testing script.

For example, you could use the Task Scheduler to run the script every 5 minutes, and in the "not responding" section you might use the send-mailmessage cmdlet to send an email when a host is not responding.

About the Author

has posted a total of 190 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links