Ping all Domain Computers with Powershell

Here’s a Powershell script that can ping all computers that are in your domain. All computers registered in AD will be pinged, including ones long dead, so this script may be useful for figuring out what is still active on your network.

There’s nothing to configure on this script so it should be good to run untouched on any domain. The console will output results of the ping but two text files will be created in your user profile folder, C:\Users\Rhys on my Windows 7 laptop. The text files will be named in the following  formats;

  • alive yyyyMMddhhmmss.csv – All computers that responded to the ping.
  • dead yyyyMMddhhmmss.csv – All computers that didn’t respond to the ping.
?View Code POWERSHELL
# Code adapted / expanded from http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx
$datetime = Get-Date -Format "yyyyMMddhhmmss";
$strCategory = "computer";
 
# Create a Domain object. With no params will tie to computer domain
$objDomain = New-Object System.DirectoryServices.DirectoryEntry;
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher; # AD Searcher object
$objSearcher.SearchRoot = $objDomain; # Set Search root to our domain
$objSearcher.Filter = ("(objectCategory=$strCategory)"); # Search filter
 
$colProplist = "name";
foreach ($i in $colPropList)
{
	$objSearcher.PropertiesToLoad.Add($i);
}
 
$colResults = $objSearcher.FindAll();
 
# Add column headers
Add-Content "$Env:USERPROFILE\alive $datetime.csv" "computer,ipAddress";
Add-Content "$Env:USERPROFILE\dead $datetime.csv" "computer,ipAddress";
 
foreach ($objResult in $colResults)
{
	$objComputer = $objResult.Properties;
	# Get the computer ping properties
	$computer = $objComputer.name;
	$ipAddress = $pingStatus.ProtocolAddress;
	# Ping the computer
	$pingStatus = Get-WmiObject -Class Win32_PingStatus -Filter "Address = '$computer'";
 
	if($pingStatus.StatusCode -eq 0)
	{
		Write-Host -ForegroundColor Green  "Reply received from $computer.";
		Add-Content "$Env:USERPROFILE\alive $datetime.csv" "$computer,$ipAddress";
	}
	else
	{
		Write-Host -ForegroundColor Red "No Reply received from $computer.";
		Add-Content "$Env:USERPROFILE\dead $datetime.csv" "$computer,$ipAddress";
	}
}

Ping all domain computers with Powershell


8 Comments

  1. Ryan says:

    Great script this is EXACTLY what I’m looking for. We haven’t cleaned out our server in a long time and currently have about 1000 computers that say their on our server when in reality only 100 or so are currently active. I’m new to powershell (and scripting in general). How do I go about running this script off powershell? In other words
    An e-mail would be preferred please.
    -Ryan

  2. admin says:

    Hi Ryan,

    Glad you like it.

    Here’s a good tutorial on running Powershell scripts

    http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx

    Rhys

  3. Tony says:

    Sweet script. I put this search filter in to only ping servers:

    # Uncomment to seach only Servers
    # $objSearcher.Filter = (”(&(objectCategory=$strCategory)(OperatingSystem=$strOS))”)

  4. Rhys says:

    Hi Tony,

    Nice addition, I’ll have to give that a whirl!

    Rhys

  5. Chris says:

    Exception calling “FindAll” with “0” argument(s): “There is no such object on the server.

    At C:\Users\cpadilla\Desktop\alive-or-dead.ps1:19 char:35
    + $colResults = $objSearcher.FindAll <<<< ();
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    I tried to modify it to search a specific OU and got the error above. Any advice appreciated:

    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    $objOU = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Workstations,dc=delacy,dc=com");
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher; # AD Searcher object
    $objSearcher.SearchRoot = $objOU; # Set Search specific OU
    $objSearcher.Filter = ("(objectCategory=$strCategory)"); # Search filter
    $objSearcher.SearchScope = "Subtree"

  6. Rhys says:

    Hi Chris,

    Sorry for the delay in getting back to you. I’ve been a bit tied up over the holiday period.

    Your code seems sound to me. I’ve managed to get a modified copy working. Probably best your check that your ldap url is correct. Specifically I’d double check to see if you have an OU called Workstations. You can check this with the following ps snippet…

    http://www.youdidwhatwithtsql.com/list-ad-organizational-units-powershell/1424

    Cheers,

    Rhys

  7. Chris says:

    That was exactly right: I needed another OU present. I’ve also included a bit that writes specific log files to a remote destination, so the script is doing a lot. It’s been running for 48 hours on 550 machines. Is there a way to track progress in the future? Enjoy the holidays! No rush on this answer, your page has already been incredibly helpful.

  8. Rhys says:

    Hi Chris,

    Depending on what you want you can probably use the Start-Transcript cmdlet to monitor progress – http://technet.microsoft.com/en-us/library/dd347721.aspx

    Or perhaps this – http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366

    Cheers,

    Rhys

Leave a Reply