Test Network Connectivity with the PowerShell Test-Connection Cmdlet

Posted on January 29, 2015 by Jeff Hicks in PowerShell with 0 Comments

Since the first time computers were networked together, people have had to test their connections. Just like a dial tone that tells you the phone is ready to use, many IT pros want to make sure they have a remote connection before beginning their ‘call’. For IT pros, this has traditionally meant using the PING utility that has existed since forever. It still works, and you can even use it in PowerShell. But when used in PowerShell, you get text output that’s hardly useful for scripting. So let me give you some additional options. Some of these cmdlets have changed through different PowerShell versions. I am testing from a Windows 8.1 desktop running PowerShell 4.0. If something doesn’t work for you, be sure to read cmdlet help and examples.

The PowerShell Test-Connection Cmdlet

The PING utility works by sending a special type of packet, an Internet Control Message Protocol echo request (ICMP), to a remote computer. Similar to the way a sonar pings another submarine, PING sends out a probe and waits for a response. The PowerShell equivalent is the Test-Connection cmdlet. This cmdlet is actually a wrapper for the Win32_PingStatus WMI class, but it’s easier to use because it’s a cmdlet. All you need to do is specify a remote computer name or IP address. The assumption is that you are testing a connection to another computer in your domain.

Using the Test-Connection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Using the Test-Connection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

By default, the cmdlet sends four pings, but you can send out more or less.

The output has been formatted for your viewing pleasure. If you pipe a connection result to Get-Member, then you’ll discover the actual property names.

Discovering property names for our test connection. (Image Credit: Jeff Hicks)

Discovering property names for our test connection. (Image Credit: Jeff Hicks)

The cmdlet allows you to specify multiple computer names:

Specifying multiple computer names with the Test-Connection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Specifying multiple computer names with the Test-Connection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

If you merely want to test a connection, then you should use the –Quiet parameter. Instead of response data, you get a simple true or false that indicates whether the computer responded to a ping request.

Using the Test-Connection cmdlet to test a connection with the -quiet parameter. (Image Credit: Jeff Hicks)

Using the Test-Connection cmdlet to test a connection with the -quiet parameter. (Image Credit: Jeff Hicks)

Here’s an example of where you might use this in a script:

This command is getting all computer accounts in the default computers container and pinging each name once. If the computer responds, PowerShell passes the ADComputer object down the pipeline, where I’m simply writing an object with a single property of Computername.

Sponsored

I could continue adding to the pipelined expression any cmdlet that accepts pipeline input by property name for Computername.

Or you might try something like this:

Using the Group-Object cmdlet to organize results in Windows PowerShell. (Image Credit: Jeff Hicks)

Using the Group-Object cmdlet to organize results in Windows PowerShell. (Image Credit: Jeff Hicks)

Using the Group-Object cmdlet, I’ve organized my results based on whether they responded or not.

Using PowerShell’s Test-NetConnection cmdlet

The Test-Connection cmdlet is included out of the box in PowerShell. If you are running Windows 8 or later, then you probably have the NetTCPIP module, which includes another command called Test-NetConnection. This command will provide additional connection information.

 The Test-NetConnection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

The Test-NetConnection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Even better, it works with external hosts. Assuming that the host is configured to respond to pings, of course.

The Test-NetConnection cmdlet in PowerShell has failed because the host isn't configured to respond to pings. (Image Credit: Jeff Hicks)

The Test-NetConnection cmdlet in PowerShell has failed because the host isn’t configured to respond to pings. (Image Credit: Jeff Hicks)

However, you can use this to test for connectivity to common ports.

Using the Test-NetConnection cmdlet in PowerShell to test for connectivity to common ports. (Image Credit: Jeff Hicks)

Using the Test-NetConnection cmdlet in PowerShell to test for connectivity to common ports. (Image Credit: Jeff Hicks)

You can test for SMB, HTTP,RDP, or a PING.

Using the Test-NetConnection cmdlet in PowerShell to test connectivity for RDP. (Image Credit: Jeff Hicks)

Using the Test-NetConnection cmdlet in PowerShell to test connectivity for RDP. (Image Credit: Jeff Hicks)

I also appreciate that I can do a trace route with this command,

Performing a trace route with the Test-NetConnection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Performing a trace route with the Test-NetConnection cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

This leaves me with this result:

The trace route was not successful. (Image Credit: Jeff Hicks)

The trace route was not successful. (Image Credit: Jeff Hicks)

Granted, this may not be the result I want, but clearly there are some devices along the way to Petri.com that won’t respond to ping requests.

Finally, this command also can be used for simple Boolean testing.

I tested to see if my NAS server is available. Here’s another example: I want to see which servers do not have Remote Desktop enabled.

Using the Test-NetConnection cmdlet in PowerShell to determine which servers that don't have Remote Desktop enabled. (Image Credit: Jeff Hicks)

Using the Test-NetConnection cmdlet in PowerShell to determine which servers that don’t have Remote Desktop enabled. (Image Credit: Jeff Hicks)

Using the Test-WSMan cmdlet in PowerShell

Most of the testing I’ve shown thus far involves ICMP pings, where the computer or firewall must be configured to allow. It is also possible that the network adapter will apply to a ping even though the operating system is hung. If you want to verify that the remote computer is ready for you, you can try using the Test-WSMan cmdlet. By this point I am assuming you have deployed PowerShell to your servers and enabled remoting. If not, you are making your life much more difficult. This cmdlet will verify that you can connect with the WSMan protocol, which means you can take advantage of the CIM cmdlets and remoting cmdlets like Invoke-Command.

Using the Test-WSMan cmdlet in PowerShell. (Image Credit: Jeff Hicks)

Using the Test-WSMan cmdlet in PowerShell. (Image Credit: Jeff Hicks)

Here’s why this might be useful. The remote computer CHI-Win81 is up and running and replies to pings.

The command has failed. (Image Credit: Jeff Hicks)

The command has failed. (Image Credit: Jeff Hicks)

But when I try to do something with it, the command fails because the WinRM service is not running on the client. It would have been better to try testing first.

Testing with the Test-WSMan cmdlet. (Image Credit: Jeff Hicks)

Testing with the Test-WSMan cmdlet. (Image Credit: Jeff Hicks)

Unfortunately, Test-WSMan doesn’t have any parameters to provide a simple Boolean response.

Sponsored

As a result, I wrote a simple wrapper function.

This command will give you a simple true or false result. If you use alternate credentials and get a false re-try with –Verbose so you can see the error message.

You now have a variety of testing tools that I encourage you to use if you are scripting a process that involves a large number of computers. As always be sure to read full help and examples for everything I’ve shown you.

Sponsored

Tagged with , ,