Test-NetConnection: A Comprehensive Guide to Network Connectivity Testing
Having a reliable network is crucial for businesses and individuals alike, making the test-netconnection command an invaluable tool for troubleshooting and ensuring seamless connectivity. Command line utilities have long been the staple for network technicians and server administrators alike for troubleshooting. Tools like a ping test and trace route command tests ping connectivity and routing hops between devices. However, the test-netconnection PowerShell module, often less known, provides tremendous troubleshooting capabilities. If you have experience with the more traditional command line tools, Test-NetConnection is a great tool to try. In this article, we will look at examples and questions.
Table of contents
Similar to Test-Connection
The Test-NetConnection cmdlet is similar to Test-Connection, which you may have used before or seen articles referencing. What is the difference? Well, Test-NetConnection is the successor to the Test-Connection cmdlet. You can see the different parameters for Test-Connection using the command:
Get-Help Test-Connection
Overview of Test-NetConnection
The Test-NetConnection PowerShell module is a great command line tool for testing network connectivity between one IP address and another between two devices. It has the advantage that PowerShell brings, such as being an object-based scripting language, meaning it is even more powerful when used in scripts. As a note, it is a different cmdlet than the test-connection tool shown above.
The Test-NetConnection cmdlet shows admins the information for a connection. It supports ping tests, TCP tests, route tracing, and it also provides route diagnostics. Depending on which parameters you use, the output can include DNS lookup name results, a list of IP interfaces, IPsec rules, route/source address selection results, and it will confirm if a connection could be established.
Basic Syntax and Command Usage
It provides the capabilities to test everything from:
- Pinging hosts for a response
- Testing Internet connectivity
- Testing ports like HTTP or SMB
- Testing latency
- Testing firewall filtering
Note the following syntax of the tool with the common parameter usage:
- Test-NetConnection -ComputerName
- Test-NetConnection -TraceRoute]
- Test-NetConnection -Hops
- Test-NetConnection -InformationLevel
Essential Features of Test-NetConnection
The Microsoft Test-NetConnection has many key features. The cmdlet supports ping test functionality, and you can test connectivity to a remote computer and analyze network health with various input parameters. Using the one test netconnection cmdlet, you can combine the functionality of multiple tools and also use the pipeline input into other cmdlets from the command.
Below is running the get-help command in the prompt:get-help test-netconnection
Note the following output of the PowerShell test-netconnection command, along with the string value options:
Get-Help Test-NetConnection
Testing Network Connectivity
If you want to run a basic network connectivity test to diagnose a connectivity error you are seeing on a client or server, you can simply type the command test-netconnection. In the example below, you can see the results of a network connectivity test, using ICMP.
test-netconnection
Note that you can use the detailed information level and receive additional information. Notice we have NameResolutionResults and the NetRoute (NextHop) displayed in addition to the information we received above.
Test-NetConnection -InformationLevel "Detailed"
Evaluating Port Availability
Now let’s take a look to see how we can evaluate port availability (seeing if a port is open or not).
Test-NetConnection -Port 443 -InformationLevel "Detailed"
The Test-NetConnection cmdlet also has a handy parameter, commontcpport, that helps to know common TCP ports without memorizing the ports they use. For instance, if you don’t remember the port number for RDP, you can use the following. Below, I am testing the connection to my domain controller, to see if the RDP port is open.
Acceptable values for this are:
- SMB
- HTTP
- RDP
- WINRM
Test-NetConnection -commontcpport <port designation> <hostname or IP>
Verifying DNS Resolution
We can test DNS resolution as well, using the same test-netconnection command with the information level set to detailed. If you know an IP on the network and want to see the DNS resolution results, you can use the command
Test-NetConnection <ip address> -InformationLevel "Detailed"
Tracing Route to a Host
Test-NetConnection allows running route diagnostics and traceroute commands. It provides detailed route diagnostics information, which is crucial for understanding network paths and identifying potential issues.
We can also use test-netconnection as a replacement for the tracert command line tool. To diagnose routing issues, we can use the command:
test-netconnection <ip address> -diagnoserouting
If you want more details on the routing paths, metrics, etc, you can add the -informationlevel “detailed”
test-netconnection <ip address> -diagnoserouting -informationlevel "detailed"
We can also add the -traceroute parameter to have the more traditional traceroute functionality from the command line:
test-netconnection <IP or hostname> -traceroute
Scripting with Test-NetConnection
You can also write quick and easy scripts that use Test-NetConnection.
This script will check the connectivity to a specified remote server and provide basic information about the connection:
# PowerShell script using Test-NetConnection
# Specify the remote host to test connectivity with
$remoteHost = "www.example.com"
# Perform the test
$testResult = Test-NetConnection -ComputerName $remoteHost
# Output the result
if ($testResult.PingSucceeded) {
Write-Host "Ping to $remoteHost successful."
Write-Host "Latency: $($testResult.Latency)ms"
} else {
Write-Host "Ping to $remoteHost failed."
}
# Optional: Add more detailed diagnostics if needed
# This will show more detailed information about the connection
# Remove the comment '#' in the next line to enable this
# $testResult | Format-List *
This script performs a basic ping test to check if the specified remote host (www.example.com
in this example) is reachable. It then outputs whether the ping was successful and shows the latency if the ping succeeded. You can replace "www.example.com"
with the actual host you want to test. Additionally, you can uncomment the last line to get more detailed information about the network test.
Test-NetConnection Key Takeways
The Test-NetConnection cmdlet is an extremely powerful tool to troubleshoot or verify network connectivity between hosts, IPs, and across the Internet. You can easily narrow in on port filtering or routing issues using the cmdlet.