PowerShell: Ping Host List from Text File

Here’s a quick PowerShell script to ping a list of hosts (computers, or other IP endpoints) from a text file. In addition, it eliminates error messages, which results in a filtered list of hosts that are alive. It runs quickly, because the ping count has been restricted to 1, from the default of 4.

Clear-Host
foreach ($Client in Get-Content -Path (Read-Host).Replace('"',''))
{
    Test-Connection -ComputerName $Client.Trim() -Count 1 -ErrorAction SilentlyContinue
}

The reason it has the call to the String.Replace() method is because, if you use the Copy as Path feature in Windows 7 Explorer to feed the input to the script, it includes double quotes around the path. In this script, we don’t actually need the double quotes, because that will confuse the Get-Content cmdlet’s –Path parameter.

The call the String.Trim() is present because in my test input text file, some computer names had whitespace after them; this call cleans that up.