Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerShell: Ping Host List from Text File

Posted by Trevor Sullivan on 2011/10/26


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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: