PowerShell – Test-Connection to Ping List of Computers

In this article, I will walk you through how to use PowerShell test-connection to ping list of computers in PowerShell. You need to use the PowerShell ping command to test for echo or response from the computer.

If you want to check multiple computers active status if they are online or offline, using PowerShell test-connection cmdlet, it is easily determined if the list of computers is reachable or not.

Test-Connection cmdlet

PowerShell test-connection cmdlet sends ICMP echo requests packets or ping one or multiple remote machines and returns echo response replies.

Syntax

Test-Connection
    [-AsJob]
    [-DcomAuthentication <AuthenticationLevel>]
    [-WsmanAuthentication <String>]
    [-Protocol <String>]
    [-BufferSize <Int32>]
    [-ComputerName] <String[]>
    [-Count <Int32>]
    [-Impersonation <ImpersonationLevel>]
    [-ThrottleLimit <Int32>]
    [-TimeToLive <Int32>]
    [-Delay <Int32>]
    [<CommonParameters>]

Test-Connection to ping multiple computers

Let’s consider an example, we have a txt file that contains a list of computers. We have a requirement to ping a list of remote computers for their status and echo their response as output.

Use below PowerShell ping script to test connection status of list of computers

$complist = Get-Content "D:\PowerShell\complist.txt"

foreach($comp in $complist){
    
    $pingtest = Test-Connection -ComputerName $comp -Quiet -Count 1 -ErrorAction SilentlyContinue

    if($pingtest){

         Write-Host($comp + " is online")
     }
     else{
        Write-Host($comp + " is not reachable")
     }
     
}

Output

incorp-as-P517 is online
incorp-as-627 is not reachable

In the above PowerShell test connection script, we have a list of computers stored in a txt file. Using Get-Content cmdlet, it reads txt file and stores list into $complist variable.

As we have to ping list of remote computer, use ForEach loop to iterate over each computer name at a time and use test-connection cmdlet to send echo request or ping.

-Count 1 parameter with test-connection cmdlet ensure to ping host remote machine only once and get a response. -ErrorAction SilentlyContinue parameter ensures that in case of host not responded, it should not throw any error and print output as the host machine is offline.

if test-connection ping status true then print response on output as ‘computer name’ is online else ‘computer name is offline’ using Write-Host cmdlet.

Cool Tip: Do you know how to print environment variables in PowerShell!

Let’s check out a few other PowerShell test connection examples and scripts to ping multiple computers using PowerShell ping script.

PowerShell Test-Connection Examples

Ping a list of host names and export to csv file

Let’s consider, you have a huge list of hostnames and you would like to test connection of list of hostnames if they are online or offline.

Using the below PowerShell ping script, you can easily ping a list of hostnames and output results to CSV file

$outputcsv = "D:\PowerShell\pingstatus.csv"
$hostlist = Get-Content "D:\PowerShell\hostlist.txt"
$OutputMessage = @()

foreach($comp in $hostlist){
    
    $pingtest = Test-Connection -ComputerName $comp -Quiet -Count 1 -ErrorAction SilentlyContinue

    if($pingtest) {
         $OutputMessage += "$comp,Online"
      }
     else {
   
       $OutputMessage += "$comp,Offline"
          
     }
     
}

 $OutputMessage | Out-File $outputcsv -Encoding utf8 -Append

In the above PowerShell ping script, read list of host names using Get-Content and use ForEach loop to iterate over for each hostname. Use test-connection to ping each computer from the list of computers.

if test-connection ping status true then store computer name and ping status as Online in $OuputMessage variable else store computer name and ping status as Offline in $OutputMessage. $OuputMessage pipe through Out-File to output the results to file and export to csv.

Cool Tip: Do you know the equivalent cat command in Windows!

PowerShell Test-Connection to send echo request to remote computer system?

using PowerShell test-connection cmdlet, send icmp echo request to remote computer.

>Test-Connection -ComputerName mycomp-517
#Ouput
Source  Destination IPV4Address  IPV6Address  Bytes    Time(ms) 
------  ----------- -----------  -----------  -----    -------- 
CORP-56 comp-517  10.217.166.17                32       7        
CORP-56 comp-517  10.217.166.17                32       8        
CORP-56 comp-517  10.217.166.17                32       18       
CORP-56 comp-517  10.217.166.17                32       9  

In the above PowerShell test connection example, the command sends echo request to mycomp-517 using ComputerName parameter specified in the PowerShell ping Test-Connection cmdlet.

Using Test-Connection to send echo requests from multiple computers to a computer

The below example sends echo requests from several computers to a single remote computer.

Test-Connection -Source corp-25, corp-server-20, corp-server-23 -ComputerName corp-server-10 -Credential shellgeek\shelladmin

in the above example, test-connection PowerShell cmdlet sends echo requests from multiple computers corp-25,corp-server-20, and corp-server-23 to a single remote computer corp-server-10 to test the latency of connections from different places.

Cool Tip: Get-AdComputer – Find computer details in OU with examples!

Using customized parameters to test connection of remote computers

The below example uses PowerShell ping test-connection parameters to test remote computer.

Test-Connection -ComputerName corp-server-25 -Count 2 -Delay 2 -TTL 255 -BufferSize 256 -ThrottleLimit 32

In the above example, test-connection uses ComputerName parameter to specify corp-server-25. It sends echo requests 2 times using Count parameter with Delay of 2 seconds interval.

How to run test-connection command as a PowerShell background job?

$job = Test-Connection -ComputerName (Get-Content hostlist.txt) -AsJob

if ($job.JobStateInfo.State -ne "Running") {$Results = Receive-Job $job}

In the above PowerShell test connection example, hostlist.txt contains list of remote host computers. Using Get-Content cmdlet, it reads all remote computers names. test-connection cmdlet parameter ComputerName takes host name from the list.

PowerShell ping test-connection cmdlet uses AsJob parameter to run ping request for list of remote computers as a background job and saves the background job in $job variable.

If command check whether the job is not in running state. If the job isn’t in running state, Receive-Job returns the results for background job and store results.

Cool Tip: Do you know how to download zip file in PowerShell!

Conclusion

In the above article, we have seen multiples examples of using PowerShell test-connection to ping list of computers, send echo requests and test connection status to your list of computer, script to ping multiple computers.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

2 thoughts on “PowerShell – Test-Connection to Ping List of Computers”

Leave a Comment