Test-Connection: Ping Remote Hosts the PowerShell Way

Test-ConnectionToday’s cmdlet of the day is Test-Connection. Test-Connection is a cmdlet that not surprisingly tests your network connection. Think of Test-Connection as PowerShell’s implementation of the popular ping utility. Even though both have ICMP in common, you’ll see that the two methods are a little different under the covers.

Using this cmdlet is simple. At it’s most basic, just specify a ComputerName parameter, and it will send four ICMP requests to the destination host.

This output looks similar to ping.exe and, on the surface, it is but Test-Connection issues the ICMP request a little differently. Unlike ping.exe, Test-Connection is using the local computer’s WMI class Win32_PingStatus to send the ICMP request. Using the local WMI repository means you’d better be sure your local WMI repository is healthy else Test-Connection will not work.

Test-Connection’s Object Output

Also, as with the beauty of PowerShell, this cmdlet doesn’t merely return what immediately shows up in the console. We see rich objects that we can gather more information from.

If I assign the output to a variable and then check out the properties, you can see that I’ve gathered a lot more useful information.

If you’re testing internal hosts, Test-Connection will use DCOM to authenticate to remote hosts. By default, it will use Packet level DCOM authentication but the authentication type can always be changed with the DcomAuthentication parameter.

Using Background Jobs

This cmdlet can also run as a background job. Background jobs come in handy if you’ve got lots of remote computers to ping and rather than wait forever for ones that’ll eventually time out, just send it to a background job. According to the PowerShell help for Test-Connection, it says that PowerShell remoting must be enabled on both the local and remote computers, but this is not true. As you can see below, I’m testing google.com, and the command still works great.

Keeping it Simple

Finally, if you’re just looking for a binary yes/no answer to if a computer is responding or not, you can always use the Quiet parameter. A common string I always use to quickly see if a server is online or not is to use Quiet and Count of 1 to force Test-Connection to send a single ICMP request.

That’s it for our cmdlet of the day! We covered the majority of what’s possible with Test-Connection but, as always, check out the PowerShell help content or head over to the Microsoft documentation for a full breakdown.

One comment

  • Excellent article. The more I use and read about PS the more interesting it becomes. Thanks for sharing your knowledge. I can see a lot of potential for practical sysadmin diagnostics just with this one “ping” replacement. Cheers!

Leave a Reply