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.



  Powershell script to ping all IP addresseses in a CSV file.
  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.
  Version:        1.0
  Author:         Open Tech Guides
  Creation Date:  12-Jan-2017
  Ping-IPList c:\IPaddressList.csv

  [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."


Consider a CSV file like below>

ip address list csv

Here is how to run the script:

PS C:\> Ping-IPList.ps1 c:\IPAddressList.csv
Reading file c:\IPAddressList.csv
Started Pinging.. Ping failed. Ping succeeded. Ping failed. Ping failed. 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


hemanth | January 9, 2020 12:51 PM |

i want to print ip address status in excel sheet using powershell script that ip address are in csv file

Pat | January 22, 2020 12:59 PM |

Here is an example: $xl=New-Object -ComObject Excel.Application $wb=$xl.WorkBooks.Open('C:\yourexcelfile.xls') $ws=$wb.WorkSheets.item(1) $xl.Visible=$true $ws.Cells.Item(1,1)= $ipaddress_status $wb.SaveAs('C:\yourexcelfile.xls') $xl.Quit()

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

JR | October 21, 2019 6:59 PM |

Necro post for those that come after me. New to this so this might not be best practice/could be ignorance, but the $ip.("IPAddress") was returning null. What worked was removing the .("IPAddress") so my command was "Test-Connection $ip" and not "Test-Connection $ip.("IPAddress")" (no quotes, obviously).

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?

Matt | October 8, 2019 5:20 PM |

Using this script, how would I have the results sent to a .csv file with the IP address and the result?

ps_guru | October 10, 2019 2:00 PM |

export-csv -path "your_file.csv" $ip.("IPAddress")

DedicatedHosting4u | April 25, 2019 8:08 AM |

Firstly, Thanks for all the useful insights. I would like to thank you for putting emphasis on how relevancy playing a big role in hosting industry. I appreciate your hard work. Keep posting new updates with us.

AndiK | April 11, 2019 11:34 AM |

Is it possible to Show the hostname and Location in result? It only Show 10.1xx.18x.x Ping succeeded. or 10.1xx.18x.x Ping failed. If it Shows 10.1xx.18x.x HostName Ping succeeded or 10.1xx.18x.x HostName Ping failed will be great.

BP | April 12, 2019 3:44 PM |

Add this line in: [System.Net.Dns]::GetHostByAddress($ip.("IPAddress")).Hostname

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