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. 172.16.0.21 is my IP address and 172.16.15.254 is my default gateway on my Wifi adapter. 13.107.4.52 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:

https://docs.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=win10-ps

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

About the author