Using test-netconnection to troubleshoot network connectivity

Whilst troubleshooting Always on VPN connectivity some time ago, I was tipped off about a good PowerShell command to help. I was trying to troubleshoot connectivity issues by using the telnet client to test for open ports because of suspected firewall issues. A bit old school, I know.

There is another way, using the PowerShell Commandlet Test-Netconnection. This has two advantages:

  • Does not require installing any additional components (e.g. telnet client)
  • Gives much more useful information and can be scripted

If we run Test-Netconnection – Informationlevel detailed we get:

I am in a hotel. is my IP address and is my default gateway on my Wifi adapter. is Microsoft endpoint for testing connectivity. So I have managed to both resolve and ping and trace a route to this endpoint;  e.g. I have an internet connection.

To go further, I can use Test-Netconnection -computername to ping something specific e.g. the BBC website:

Or trace a route to that particular resource by adding -traceroute to the end of the above command, or test if a port is open to that resource:

This command can also fairly easily be used in scripts, and we can do some more advanced operations:

Does my server have port 80 open (common ports in this command are RDP, HTTP, SMB, WINRM)

Test-Netconnection SERVERNAME -CommonTCPPort http -InformationLevel Quiet

If yes, the output will be True

Also, we can use simple Boolean to investigate what servers in my environment have or do not have certain ports open. Show me from a few servers which does not have port 80 open:

“SERVER1″,”SERVER2” | where { -NOT (Test-Netconnection $_ -CommonTCPPort http -InformationLevel Quiet)}

Would return the names of any servers which do not have http open.

I thought it was a pretty useful Commandlet!

The Microsoft documentation for this command is here:

and PowerShell v4 / Windows 8 / Windows 2012 R2 is required.

About the author