How to Use the Test-NetConnection in Powershell

In the realm of network troubleshooting, having the right tools and knowledge can save you time and headaches. PowerShell offers a robust Test-NetConnection cmdlet that serves as your all-in-one network diagnostic tool.

With its capabilities, you can efficiently troubleshoot connectivity issues, verify port availability, and ensure DNS resolution is working correctly.

Whether you’re a seasoned IT professional or a network engineer, mastering this command equips you with the power to maintain seamless network operations.

In this article, you'll learn how to use Test-NetConnection, explore its practical applications and discover ways to leverage its full potential for enhanced network diagnostics.

What is Test-NetConnection in PowerShell?

Test-NetConnection in PowerShell is a perfect tool designed for comprehensive network diagnostics.

It's primarily used for testing network connectivity, checking TCP connectivity to a network port, performing DNS resolution, and identifying network routing issues via trace route command.

This cmdlet provides detailed ping and route diagnostics, making it invaluable for troubleshooting network performance issues.

Test-NetConnection Syntax

The basic syntax for the PowerShell Test-NetConnection cmdlet as per official Microsoft documentation is:

Test-NetConnection [-ComputerName <String>] [-Port <Int32>] [-InformationLevel {Detailed | Quiet}] [-TraceRoute] [-CommonTCPPort {HTTP | SMB | RDP | WINRM}] [-Hops <Int32>] [-DiagnoseRouting] [<CommonParameters>]

Parameters include:

  • -ComputerName: Specifies the DNS names or IP address of the destination address to test connectivity.
  • -Port parameter: Specifies the TCP port on the remote connection to test connectivity. If no port is specified, the cmdlet performs a basic ICMP echo check.
  • -InformationLevel: Controls the info level of the output. Two acceptable values are:
  • Detailed: Displays comprehensive output including PingReplyDetails, resolved IP addresses, and network routing details
  • Quiet: Returns a True/False confirmation of connection establishment
  • -TraceRoute: Executes a demo of trace route, showing the path through intermediate network devices
  • -Hops: Limits the number of hops during traceroute operations for analyzing network bottlenecks
  • -CommonTCPPort: Tests connectivity for network port settings on default server ports:
  • HTTP (port 80)
  • SMB (port 445)
  • RDP (port 3389)
  • WINRM (port 5985)
  • -DiagnoseRouting: Provides route selection diagnostics when troubleshooting network infrastructure issues

3 Common Case Uses for the 'Test-NetConnection' Command

The Test-NetConnection cmdlet in PowerShell is a versatile tool, making it indispensable for systems administrators and network professionals.

Here are three specific use cases where you will need it:

  1. Testing Network Connectivity to Remote Servers: Verify ping connectivity to a host to ping using ICMP pings, providing more detailed error messages than traditional PING utility.
  2. Verifying Port Status: Monitor network traffic on specific ports, crucial for maintaining network security settings.
  3. Network Route Diagnostics: Use the trace route command to analyze network adapter paths and identify potential network bandwidth bottlenecks.

Test-NetConnection vs Test-Connection cmdlet

When it comes to network diagnostics in PowerShell, both Test-NetConnection and Test-Connection are frequently used utilities, yet they serve different purposes and capabilities.

The Test-Connection cmdlet can be considered as an advanced version of the classic ping command. It essentially sends Internet Control Message Protocol (ICMP) echo requests to check the reachability of a remote computer and measure the network latency. It provides basic connectivity status, helping you swiftly determine if a host is up or down.

On the other hand, Test-NetConnection is a much more comprehensive tool, offering a broader scope for network diagnostics. While it can perform tasks similar to Test-Connection by checking the reachability of hosts, it goes beyond just ICMP.

This cmdlet supports testing for TCP connections, thus allowing you to specify port numbers and validate port accessibility on a target host.

Furthermore, it equips you with additional diagnostic capabilities such as DNS resolution and trace route operations to better analyze the network path and latent bottlenecks.

Essentially, while Test-Connection is limited to basic ping tests, Test-NetConnection provides a more detailed and versatile diagnostic report, making it an ideal choice for advanced network troubleshooting scenarios.

Feature Comparison

Feature

Test-Connection

Test-NetConnection

Basic Ping Test

Yes

Yes

Specific TCP Port Test

No

Yes

Route Diagnostics

No

Yes

Detailed Network Information

No

Yes

Scope of Diagnostics

Limited

Comprehensive

When to use which:

  • Use Test-Connection for basic functions like ping requests and response time monitoring
  • Choose Test-NetConnection for advanced network management tasks requiring detailed ping results and TCP connectivity verification

How to use Test-NetConnection Cmdlet: 5 Examples

Here are five examples with code implementation to demonstrate its commonly-used functionality:

Example 1: Basic Network Connectivity Test

To test basic connectivity to a remote system, use the Test-NetConnection cmdlet with the -ComputerName parameter:

PS C:\>Test-NetConnection -ComputerName google.com

This will return the status of the network connection to the specified hostname, including the ping test result.

Example 2: Using TraceRoute

To identify network hops and potential bottlenecks, use the -TraceRoute parameter. This provides detailed information about the path your data takes to reach the destination:

Test-NetConnection -ComputerName google.com -TraceRoute

You can also limit the number of hops displayed using the -Hops parameter. For example, to limit the trace to 5 hops:

Test-NetConnection -ComputerName google.com -TraceRoute -Hops 5

Example 3: Port Status Verification

To verify connectivity to ports, you can use two approaches:

  • Check Common TCP Ports: Use the -CommonTCPPort parameter to quickly check predefined common ports. For example, to check if HTTP (port 80) is open:

    Test-NetConnection -ComputerName google.com -CommonTCPPort HTTP
  • Verify Specific Port Status: To check a specific port number that isn't a common TCP port, use the -Port parameter. For instance, to see if port 443 (HTTPS) is open:


Test-NetConnection -ComputerName google.com -Port 443

This combination allows for both rapid testing of standard ports and detailed checks of any specified port.

Example 4: Network Route Diagnostics

To check for any routing problems, utilize the -DiagnoseRouting parameter. This command will attempt to identify potential routing issues when connecting to the specified hostname:

Test-NetConnection -ComputerName google.com -DiagnoseRouting

You can also constrain the source address or interface using the -ConstrainSourceAddress and -ConstrainInterface parameters. For example:

Test-NetConnection -ComputerName google.com -DiagnoseRouting -ConstrainSourceAddress 192.168.1.1

Example 5: Information Level Control

To receive different levels of information about the connection, you can use the -InformationLevel parameter. For instance, to get detailed information, you can combine it with any previous commands:

Test-NetConnection -ComputerName google.com -TraceRoute -InformationLevel Detailed

Final Note

These examples demonstrate how Test-NetConnection can streamline your network troubleshooting tasks without relying on third-party tools. The online community consistently rates it as one of the most valuable tools for administrators in the PowerShell toolkit.

Remember, maintaining a solid understanding of these network diagnostics tools is essential for efficient IT operations and minimizing human error in your network infrastructure management. Happy diagnosing!