Use the PowerShell Test-Connection cmdlet to check multiple computers active status if they are online or offline, it is easily determined if the list of computers is reachable or not.
In this article, we will discuss 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.
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 text file.
Using the Get-Content cmdlet, it reads the txt file and stores the list into $complist
variable.
As we have to ping a list of remote computer, use ForEach
loop to iterate over each computer name at a time.
Use the test-connection cmdlet to send an echo request or ping.
-Count
1 parameter with test-connection
cmdlet ensures to ping the host remote machine only once and gets a response.
-ErrorAction SilentlyContinue parameter ensures that in case of host not responding, it should not throw any error and print output as the host machine is offline.
If the test-connection ping status is true then print the 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 it to CSV file
Let’s consider, you have a huge list of hostnames and you would like to test connection of the 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 a 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 the list of host names using Get-Content
and use ForEach
loop to iterate over 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 the PowerShell test-connection cmdlet, it sends an ICMP echo request to the 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 an 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, the 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 the 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 a list of remote host computers.
Using Get-Content
cmdlet, it reads all remote computers names. test-connection cmdlet parameter ComputerName takes the hostname from the list.
PowerShell ping test-connection
cmdlet uses AsJob
parameter to run a ping request for a list of remote computers as a background job and saves the background job in $job
.
The command checks whether the job is not in a running state. If the job isn’t in a running state, Receive-Job
returns the results for background job and store results.
Cool Tip: Do you know how to download a zip file in PowerShell!
Conclusion
In the above article, we have seen multiple examples of using PowerShell test-connection
to ping a list of computers, send echo requests, and test connection status to your list of computers, and script to ping multiple computers.
Test-Connection cmdlet is also used to perform ping sweep in PowerShell.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.
Thanks for the script! This is exactly what I was looking for and it works perfectly.
Perfect. Thank you for the simplicity of your explanation and to the point. Now I am searching foor more help.