I created the following script to check some VM’s at home. This script pings all the servers from the CSV file. If a ping has failed, the script will send an e-mail with the servername and ip-address.

If you want to use this script, you have to change/create the following items:

  • Create a CSV file with your servers and ip-addresses
  • Enter your SMTP Server and e-mail address in the powershell script
  • Schedule the powershell script


The CSV file will contain the following information:



The Powershell script:

$servers = "D:\scripts\ps\servers.csv"
$csv = Import-CSV $servers 
$smtpServer = ""

foreach($item in $csv){
    $server = $item.ServerName
    $ip = $item.IpAddress

    $ping = new-object System.Net.NetworkInformation.Ping
    $rslt = $ping.send($ip)
        if ($rslt.status.tostring() –eq "Success") {
            write-host ping worked on $server with $ip -ForegroundColor Green}
        else {
            write-host ping failed on $server with $ip -ForegroundColor Red
            # Send E-Mail
            $msg = new-object Net.Mail.MailMessage
            $smtp = new-object Net.Mail.SmtpClient($smtpServer)
            $msg.From = "@"
            $msg.Subject = "Warning: ping failed on $server"
            $msg.Body = "The server $server with ip adres $ip does not reply!"
$ping = $null

The source of the ping script:


9 thoughts on “Powershell script to ping servers

  1. Doesn’t work

    Exception calling “Send” with “1” argument(s): “Value cannot be null.
    Parameter name: hostNameOrAddress”
    At C:\srvping\servers.ps1:10 char:23
    + $rslt = $ping.send( <<<< $ip)
    You cannot call a method on a null-valued expression.
    At C:\srvping\servers.ps1:11 char:34
    + if ($rslt.status.tostring( <<<< ) -eq "Success") {

    1. I got the same error as Vig.
      Turns out it was caused by a space after the s at the end of ServerName,IpAddress in the .csv file
      delete the space and all works well.

      Thanks for the script.

  2. Is there a way to have the output written to a .txt or .csv file? I have been able to create the file, but it is blank.

  3. Here’s an alternative that can write output to a CSV file. It can also process hundreds or even thousands of network addresses in record time and do a Name Server lookup to obtain fully qualified hostname and IpAddress:

    Check it out here:

    It does not send email, but you could easily do that using a secondary step using the code above or the Send-MailMessage cmdlet.

  4. Hi folks, does anyone know how to change the number of echo requests sent before a success or failure result is logged (and hence emailed.) Ideally I’d like it to email only if it drops 10 icmp packets in a row.

    #this script generate a list of ip addresses and then
    #creates a file that contain hostnames agianst that ip addresses using the ping command

    #get a list of ip addresses

    $ips = 1..254 | % {“172.17.17.$_”} > iplist.txt
    #ip to host name
    foreach ($ip in $data) {
    $ipg = ping -a -n 1 $ip | Select -Index 1
    $ipg.substring(8,$ipg.length-30) >> listofservers.txt

  6. Pingback: Viktoria

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.