Pinging a device is a basic skill of any system administrator, and we all know the ping.exe command but have you heard of the PowerShell commands Test-Connection and Test-NetConnection? It's PowerShell's way of injecting the old-school ping.exe command with steroids! Let's break out some PowerShell to test a remote connection and more in this blog post.

Pinging a computer is really easy to do in PowerShell but there are a set of options that really make it useful, and take it way beyond what ping can do.

PowerShellified Ping?

Although old-school ping is a legacy tool, it's still around and can be invoked with PowerShell just like any other cmd utility can. It can be executed either directly by typing ping <hostname> or you can also use the Invoke-Expression cmdlet to call ping.exe but this is just putting lipstick on a pig.

PS51> Invoke-Expression -Command "ping.exe google.com"
ping.exe

This works and you'll get the normal ping results back, but it's a bit clunky and all you get is the text output from the ping command. Let's use the native PowerShell cmdlet Test-Connection instead.

Test-Connection

PS51> Test-Connection www.google.com
PowerShell Test-Connection

That's a much more native way of doing it in PowerShell, and it returns an object rather than just the text output of ping.exe.

If you pipe the object coming from the PowerShell Test-Connection cmdlet into Select-Object -Property * we get a mass of useful information.

PS51> Test-Connection www.google.com | Select-Object -Property *
PowerShell Test-NetConnection

Like any good PowerShell cmdlet we have switches so we can set things like Count for the number of attempts, BufferSize for the size of the packet and Delay to define the delay between each attempt and use PowerShell to test a remote connection like a boss.

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

There are a lot of other parameters you can use but I'm not going to go through them all. But there are some parameters that are useful like Source. �The Source parameter makes it possible to use the PowerShell Test-Connection cmdlet to connect to other machines on your network and initiate connection attempts from there.

PS51> Test-Connection -Source "LocalHost", "TestVM01", "TestVM02" -ComputerName "www.google.com"
Using Source Test-NetConnection

The output shows all the results from the hosts in the source list, in one nice neat table containing objects. �This is especially useful if you have a complicated network with lots of firewalls between you and the target. �As long as you can get to those source machines then you can test any of the connections from there.

The Quiet parameter goes the other way and gives a really simple true/false result. �This is super useful when using it in if statements.

PS51> Test-Connection www.google.com -Quiet
PowerShell Test-Connection Quiet

If you have a lot of targets to test then the AsJob parameter might be useful for putting the list to a background job and getting the results using Get-Job | Receive-Job.

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

Test-NetConnection

Another cmdlet to look at is Test-NetConnection. �The Test-NetConnection cmdlet can test the connection to a device much like the PowerShell Test-Connection cmdlet but it's a little more networking focused. �In the simplest sense, it gives much the same results.

PS51> Test-NetConnection www.google.com

Again this cmdlet has a load of really useful parameters like Port to test whether a remote port is open or not.

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

With the TraceRoute parameter you can do the same as you would with tracert.exe, but the output is a PowerShell object with each of the hops on the route to the target.

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

Again, if you want to use PowerShell cmdlet Test-NetConnection in an if statement to test if a device has port 80 open you can use the -InformationLevel Quiet parameter/value to give you a simple true/false result from the test.

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

If you'd like an advanced example of using these cmdlets, I highly encourage you to check out the post An All-in-One PowerShell Server Port Testing Tool.

Summary

Whether you choose to use the PowerShell Test-Connection cmdlet or Test-NetConnection cmdlet, we've got you covered! Using one or both of these cmdlets will allow you to use PowerShell to test a remote connection with no problem at all many different ways!

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!