How-to: Ping computers with PowerShell [psp]

How to ping computers to test for network connectivity.

If you only need to ping a single machine with Test-Connection then using the -quiet parameter will return a True/False value that can be used as part of a simple If statement:

If (Test-Connection remotehost -count 1 -quiet) {
  Write 'The host responded'
  # Add other statements here..
}

The -count 1 means that it will only attempt to contact the machine once.

Timeout

When pinging a host that doesn't exist or is offline there will be a delay of some seconds before it is returned as 'host not found'.

The PING -w and the WMI Get-CIMInstance Win32_PingStatus timeout=NNN options both offer to control the Timeout, but this is the ICMP timeout not the DNS client timeout.

When sucessfully contacting a host, the ICMP timeout is used (default typically 1-4 seconds) but when attempting to PING a non-existent or offline computer it will be a DNS timeout causing the delay, typically around 9 - 10 seconds.

The time to respond as 'host not found' is for the first response, once you have pinged a host once, DNS will cache the (non) response and respond much faster to a second PING.

The performance of PING, Get-CIMinstance, PSPING and Test-Connection are all comparable, the only difference being that PING and Get-CIMinstance allow modifying the ICMP timeout.

Function to Ping multiple Hosts

The function below may be used two ways:
type psp remotehost on the command line, or alternatively pipe several host/computer names: "host1" ,"host2" | psp

This function uses Get-CIMinstance so that you can adjust the timeout and also for PowerShell 6/7 users who don't like the redesigned Test-Connection. The function is named psp rather than ping because you may still wish to run ping.exe from the PowerShell command prompt. Examples follow below.

Function psp {
   param($InputObject = $null)

   BEGIN {$status = $True}

   PROCESS {
      if ($InputObject -and $_) {
         throw 'ParameterBinderStrings\AmbiguousParameterSet'
      } elseif ($InputObject -or $_) {
         $processObject = $(if ($InputObject) {$InputObject} else {$_})

         write-host "Ping [$processObject]"
         $result = Get-CIMinstance win32_pingstatus -Filter "address = '$processObject' and Timeout=1000"

         if ($result.StatusCode -eq 0) {
           write-host "Ping response OK" -ForegroundColor DarkGreen
             # write-host $result.protocoladdress
         }
         else {
            write-host "Ping failed - host not found" -ForegroundColor red
            $status = $False
         }
      }
      else {throw 'ParameterBinderStrings\InputObjectNotBound'}

   # next processObject
   }

   # Return True if pings to all machines succeed:
   END {return $status}
}

This function will return a True or False value, $True will only be returned if the pings to ALL the machines succeed.

Note: PowerShell does not save functions or aliases permanently by default. So if you close and reopen PowerShell, this function will no longer be available. To make it permanent, add the function to your PowerShell $Profile file.

Examples

Ping server64:
PS C:\> psp server64

Ping three servers:
PS C:\> "server64" ,"server65" , "10.164.199.200" | psp

Ping a list of workstations:
PS C:\> $workstations = cat E:\MachineNames.txt
PS C:\> $workstations | psp

or using foreach:
PS C:\> foreach($workstation in $workstations){psp $workstation}

Perform a command only if the machine server64 responds to a ping:

if (psp "server64"){
   echo "responding"
   } else {
   echo "machine not found"
   }

“And now I see with eye serene
The very pulse of the machine” ~ William Wordsworth, (She Was a Phantom of Delight)

Related PowerShell Cmdlets:

Test-Connection - Ping one or more computers.
Test-NetConnection - Display diagnostic information for a connection.
Functions and Filters - Write a named block of code.
Write-Warning - Write a warning message.
PSPING - Sysinternals Ping Tool.


 
Copyright © 1999-2021 SS64.com
Some rights reserved