Using PowerShell’s Test-Connection and Test-NetConnection Cmdlets

ThePowerShell Test-Connection and Test-NetConnection cmdlet are the new way of pinging remote computers but we’ve still got good ol’ ping.exe.

Pinging a device is a basic skill of any ‘IT Pro’, and we all know the Ping.exe command but have you heard of the ? It’s PowerShell’s way of reinventing the old-school ping.exe command.

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.

We could just use Invoke-Expression to call the ping.exe but this is just putting lipstick on a pig.


Are you an IT pro ready to take your career to the next level? If so, join our contributor program! You don't have to be an expert presenter, a Microsoft MVP or even have a blog. We want your knowledge! You will be coached on presentation skills, become a member of our awesome community and get paid some nice side hustle income every, single month! Sign up today!.

ping.exe

Yeah, this works, and we’ll get the normal ping results back of course, but it’s a bit clunky and all we get is the text output from the ping command, so let’s use the native PowerShell cmdletTest-Connection instead.

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.  So, if we pipe the object coming from the PowerShell Test-Connection cmdlet into Select * we get a mass of useful information.

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.

There are a lot of other switches of course, and I’m not going to go through them all but there are some that are really useful like Source.  This makes it possible to use the PowerShell Test-Connection cmdlet to connect to other machines on your network and initiate connection attempts from there.

Using Source Test-NetConnection

The output shows all the results from the hosts in the source list, in one nice neat table (object).  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 switch goes the other way and gives a really simple true/false result.  This is super useful when using it in If statements.

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.

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.

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

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

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

Whether you choose to use the PowerShell Test-Connection cmdlet or Test-NetConnection cmdlet, we’ve got you covered!

I’m a freelance SysAdmin with 20 years experience in IT. My focus is mainly on PowerShell, Automation and Azure Infrastructure. I’ve always had a fascination for anything techie and love learning and sharing that knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.