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

4 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

Leave a Reply