PowerShell to ping a list of IP addresses in a CSV file

Posted on 12th January 2017

This PowerShell script reads a list of IP addresses or host names from a CSV file, Ping each of them and report the results. The filename and path of the CSV file to read is passed to the script as a parameter. The script gets the IP addresses from a column in CSV file with header "IPaddress". If you want to use a different column, simply change the variable $ColumnHeader in the script.

The main PowerShell cmdlets used in this script are Import-CSV to read the CSV file and Test-Connection to ping the IP addresses.

Ping-IPList.ps1

<#

.SYNOPSIS
  Powershell script to ping all IP addresseses in a CSV file.
  
.DESCRIPTION
  This PowerShell script reads a CSV file and pings all the IP addresses listed in the IPAddress column.
  
.PARAMETER <csvfile>
   File name and path of the CSV file to read.
 
.NOTES
  Version:        1.0
  Author:         Open Tech Guides
  Creation Date:  12-Jan-2017
 
.LINK
    www.opentechguides.com
    
.EXAMPLE
  Ping-IPList c:\IPaddressList.csv
   
#>

Param(
  [Parameter(Mandatory=$true, position=0)][string]$csvfile
)

$ColumnHeader = "IPaddress"

Write-Host "Reading file" $csvfile
$ipaddresses = import-csv $csvfile | select-object $ColumnHeader

Write-Host "Started Pinging.."
foreach( $ip in $ipaddresses) {
    if (test-connection $ip.("IPAddress") -count 1 -quiet) {
        write-host $ip.("IPAddress") "Ping succeeded." -foreground green

    } else {
         write-host $ip.("IPAddress") "Ping failed." -foreground red
    }
    
}

Write-Host "Pinging Completed."

Example

Consider a CSV file like below>

ip address list csv
IPAddressList.csv

Here is how to run the script:

PS C:\> Ping-IPList.ps1 c:\IPAddressList.csv
Reading file c:\IPAddressList.csv
Started Pinging..
192.168.1.98 Ping failed.
192.168.1.64 Ping succeeded.
192.168.1.220 Ping failed.
192.168.1.5 Ping failed.
192.168.1.21 Ping succeeded.
Pinging Completed.

With some minor changes you can extend the functionality of this script a lot further. For example instead of pinging all the IP address in the list if you want to ping only the devices in a particular location, you only need to add a Where-Object cmdlet to Import-csv as below

$ipaddresses = import-csv $csvfile | where-object {$_Location -eq "Office 1"} | select-object $ColumnHeader 

The above line will get only the IP address that are in "Office 1" location.

This script is free of any copyrights, so feel free to customize and share your experience with us ;)


Post a comment

Comments

gt | September 19, 2018 4:16 PM |

getting error.
Test-Connection : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again. At \\server\user\PS Scripts\PING IP CHECK.ps1:36 char:25 + if (test-connection $ip.("IPAddress") -count 1 -quiet) { + ~~~~~~~~~~~~~~~~~ + CategoryInfo

Chico | September 27, 2018 4:59 PM |

Issue must be at this line:
$ipaddresses = import-csv $csvfile | select-object $ColumnHeader
Are you getting any value in the $ipaddresses variable?

spiro | August 31, 2018 11:37 PM |

This is very helpful! Would it be difficult to output the response times for successful pings as well?

michael | July 17, 2018 6:03 PM |

How do i get it to print the Hostname Along side with the IP address

calvin | March 16, 2017 10:42 AM |

Great stuff. How to output to a excel file and with the Hostname tied to the IPAddress?

Nevin | January 5, 2018 10:00 AM |

Use the Export-csv cmdlet to write the result to a csv file

Armando Padilla | March 14, 2017 12:38 PM |

Very good