fbpx

Use PowerShell Test-Connection | Check connectivity in Remote Computer In 3 Best ways

Use the PowerShell Test-Connection cmdlet to quickly discover whether the list of computers is reachable or not by checking the active state of numerous machines to see if they are online or offline.

Test-Connection

Internet Control Message Protocol (ICMP) echo request packets are sent to the distant computers by the PowerShell cmdlet Test-Connection. receives the echo-response in return and uses it to determine if the distant computers are active, connected to the network, or both. 

How To Use Powershell Test-Connection?

  1. Basic Ping Command
  2. The Conventional Way: Test-Connection Cmdlet
  3. How to use Test-NetConnection?

1. Basic Ping Command To check Connectivity

Despite being an outdated tool, old-school ping is still usable and can be used with PowerShell just like any other cmd utility. 

It can be run directly by entering the ping <hostname> parameter. You can also use the Invoke-Expression cmdlet to call ping.exe. It is much different than test-connection command.

Basic Ping Command To check Connectivity

An Internet Control Message Protocol echo request packet (ICMP), a unique kind of packet used by the PING application, is sent to a distant computer. PING broadcasts a probe and awaits an answer. The Test-Connection cmdlet is the PowerShell equivalent. As you can see it simply displays 32 byte data transfer without computer names.

2. The Conventional Way: Test-Connection Cmdlet

Test-Connection www.google.com
Test-Connection www.google.com

That returns an object rather than just the text output of ping.exe, which is a manner of handling it in PowerShell that is much more native.

We can acquire a ton of helpful information if we route the object coming from the PowerShell Test-Connection cmdlet into Select-Object -Property *.

Test-Connection www.google.com | Select-Object -Property *
Test-Connection www.google.com | Select-Object -Property *

Like any good PowerShell cmdlet, this one has switches that allow us to define parameters like Count for the number of tries, BufferSize for the size of the packet, and Delay for the amount of time to wait between tries. This allows us to utilize PowerShell to test a remote connection like a boss.

Test-Connection www.google.com -Count 2 -BufferSize 128 -Delay 3
Test-Connection www.google.com -Count 2 -BufferSize 128 -Delay 3

There are other additional parameters you can use, but I won’t list them all here. However, some parameters, such as Source, are helpful. The PowerShell Test-Connection cmdlet allows you to connect to other computers on your network and launch connection attempts from there thanks to the Source option.

Test-Connection -Source "LocalHost", "TestVM01", "TestVM02" -ComputerName "www.google.com"
Test-Connection -Source "LocalHost", "TestVM01", "TestVM02" -ComputerName "www.google.com"

The output displays every result from every host in the source list in a single, orderly table with objects. This is especially helpful if there are numerous firewalls and a convoluted network between you and the target. 

The Quiet parameter flips the situation and returns a pretty straightforward true/false answer. When included in if statements, this is really beneficial.

Test-Connection www.google.com -Quiet
Test-Connection www.google.com -Quiet

If you have a large number of targets to test, the AsJob option may be helpful for running the list as a background job and then using Get-Job | Receive-Job to retrieve the results.

Test-Connection google.com -count 10 -AsJob
Get-Job | Receive-Job
Test-Connection google.com -count 10 -AsJob
Get-Job | Receive-Job

3. How To Use Test-NetConnection Cmdlet?

You should also take a look at Test-NetConnection command. Similar to the PowerShell evaluate-Connection cmdlet, the Test-NetConnection cmdlet may evaluate a device’s connection, but it is a little more networking-focused. you can use it to check on a remote computer.

Test-NetConnection www.google.com
Test-NetConnection www.google.com

Again, there are a ton of incredibly helpful arguments in this cmdlet, such as Port, which can be used to check whether a remote port is open or not. it displays the IP address and latency

Test-NetConnection www.google.com -Port 80
Test-NetConnection www.google.com -Port 80

You may accomplish the same task with the TraceRoute argument as you would with tracert.exe, but the output is a PowerShell object that lists each hop in the route to the target.

Test-NetConnection www.google.com -TraceRoute
Test-NetConnection www.google.com -TraceRoute

If you want a straightforward true/false response from the test, you may use the -InformationLevel Quiet parameter/value with the PowerShell cmdlet Test-NetConnection to check if a device has port 80 open.

Test-NetConnection www.google.com -port 80 -InformationLevel Quiet
Test-NetConnection www.google.com -port 80 -InformationLevel Quiet

I strongly advise you to read the post An All-in-One PowerShell Server Port Testing Tool if you’d like an advanced example of using these cmdlets.