Before PowerShell Test-NetConnection, we had a wide selection of command-line tools at our disposal to troubleshoot network connectivity problems. Among other utilities, we had ping to test ICMP echoes and replies; tracert to see where our packets might be dropped; nslookup to query our DNS servers; telnet for testing TCP ports; and others. Everything had a utility.
This single-utility-for-a-single-task approach to troubleshooting connectivity problems has been rendered obsolete with the introduction of PowerShell v4 on Windows 8.1 and Windows Server 2012 R2. PowerShell Test-NetConnection is a powerful new cmdlet that I would like to introduce to you. Consider PowerShell Test-NetConnection as ping, tracert, nslookup, telnet, and a few other utilities all rolled into one troubleshooting tool.
Take a look at how the PowerShell Test-NetConnection cmdlet can be used when you're in the unfortunate position of troubleshooting a network connectivity problem. I will demonstrate this by using PowerShell Test-NetConnection to troubleshoot a common, real-world issue: "I cannot access the XYZ website!"
In fact, most people don't realize that successfully rendering a website in a browser is in itself an amazing feat considering the number of moving parts that have to work together. At a minimum, the process involves:
You'll first need to confirm that you have an internet connection before you begin troubleshooting. You can do this by simply running PowerShell Test-NetConnection without any parameters. If you need more information, I recommend using the InformationLevel parameter with the Detailed argument.
Test-NetConnection -InformationLevel Detailed
Using this command, you can check your local connectivity and internet connectivity and confirm that your DNS client can resolve names directed at your DNS server simultaneously. Think of it as a general health check for your network. In one fell swoop, this command checks three of the five processes required to render a website.
We will now have to direct our troubleshooting to the website host in question. Let's take Google.com as an example. With Test-NetConnection and the ComputerName parameter, we can simultaneously test that the website host can be resolved in DNS, that a TCP route exists to get to the IP address that the name resolves to, and that the website can be ping-ed.
Test-NetConnection -ComputerName google.com
While this step technically verifies there is a route to the google.com web server, I want to find out more information about which routers my packets are flowing through to reach the google.com web server. I will use the TraceRoute parameter to get a list of routes.
Test-NetConnection -ComputerName google.com -TraceRoute
We will ensure that the TCP port on which we expect the web server to run is open in our final test. Due to the fact that we only specified google.com, I'm assuming it's TCP port 80 in this case. Adding another parameter to Test-NetConnection will accomplish this. We don't even have to know the port number since Test-NetConnection understands the standard TCP port for a few different services. Using the CommonTCPPort parameter, I can just pass HTTP and it will handle everything.
Connect-Network -ComputerName google.com -CommonTCPPort HTTP
You can specify a TCP port directly by using the Port parameter, however, if the website might be running under a different port, such as 8080.
Connect-Network -ComputerName google.com -Port 80
Each of the connectivity requirements outlined at the beginning of this article has now been tested. We can submit the problem to Google or possibly a downstream DNS server if we are still unable to render the website at this time. For more PowerShell tricks, read our eBook, "How to Automate Using PowerShell."