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.
# 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";
}
}