404 Tech Support

PowerShell script to ping multiple computers

A recent project involved tracking down some old computers. To find them, we only had the computer name to start. We came up with a plan to try to find out more information about the computers by accessing the hidden C$ share on the computers. This quickly became time-consuming and frustrating as many of the computers were not even pingable, as they were most likely not powered on at the time we were trying to connect to them.

To speed things up, we could ping each computer first and only try the more time-consuming C$ share approach to those that responded to the ping. Since these computers might be powered on at different timesĀ of the day, a script made the most sense to save from typing everything out each time.

This PowerShell script reads from a text file ping.txt with each computer name on its own line and then uses test-connection to ping the computer. Ping.txt should reside in the same folder as the script. The computers that are successfully pinged are pinged one more time to get their IP address (more information to use in tracking them down) and are logged in a text file to the desktop as text file named pingable_[timestamp]. The computers that do not reply to the ping are logged to a text file on the desktop named notpingable_timestamp.

I considered making it a circular log so the non-responding computers would be filtered and ready for the next round of running the script but kept with this direction to keep things more straight-forward.

Note to replace .domain.fqdn with your own fully qualified domain name, so it is appended to each computer name.

# Jason Hamilton
# https://www.404TechSupport.com/
# Ping a number of computers and report the successful ones.

# List computer name (not FQDN) in ping.txt in the same directory as this script
$ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Write-Host $ScriptPath
$PCs = Get-Content $ScriptPathping.txt
$timestamp = get-date -f "MMddyyyyHHmm"
$successlog = [string]::concat($Env:userprofile, "Desktoppingable_", $timestamp,".txt")
$faillog = [string]::concat($Env:userprofile, "Desktopnotpingable_", $timestamp,".txt")
Clear-host
Write-Host "Testing connection to computers listed in ping.txt"
Write-Host ""

# loop through above array of each computer and test pinging to them.
Foreach ($i in $PCs)
{
$PC = $i+".domain.fqdn"

$pingable = test-connection -ComputerName $PC -Count 2 -quiet

if ($pingable) {
$pingable2 = test-connection -ComputerName $PC -Count 1

#List successful connections
Write-Host $PC "is pingable with" $pingable2.IPV4Address
$report = [string]::concat($PC, " is pingable with ", $pingable2.IPV4Address)
$report | Out-File -FilePath $successlog -Append

} else {
Write-Host $PC "was not pingable."
$report = [string]::concat($PC, " was not pingable.")
$report | Out-File -FilePath $faillog -Append
}
}

Write-Host ""
#Equivalent to Pause
Read-Host 'Press Enter to continue...' | Out-Null

After the script completes going through the list of computers, you would be able to take the pingable text file and connect to those C$ shares and drill down to the user profile folder to find out who has logged in to the computer recently and hopefully help locate the computer.