Test-NetConnection Cmdlet: A PowerShell-Based Ping AlternativeTest-NetConnection Cmdlet: A PowerShell-Based Ping Alternative

PowerShell's Test-NetConnection cmdlet offers more advanced capabilities than the ping command. This tutorial demonstrates the cmdlet's most useful features.

Brien Posey

February 25, 2025

4 Min Read
a section of a poweshell session demonstrating the test-networkconnection cmdlet against an abstract tech background

Ping is probably the most basic of all network troubleshooting tools, yet it remains widely used. Most people, including myself, use it by entering the ping command followed by a hostname or IP address. The tool sends packets to the designated host and listens for a response (see Figure 1). As such, IT professionals often use ping to check if two hosts can communicate.

powershell terminal displays a demonstration of a basic ping command

Figure 1. Ping sends packets to a designated host and listens for a reply.

The Limitations of Ping

While useful, the ping command is extremely limited. Created in 1983, it has seen occasional upgrades, but its core function remains unchanged.

The ping -? command is run in a PowerShell terminal to display the available ping command options

Figure 2. Run ping -? to display the available options for ping. The Windows version of the ping command supports multiple command-line switches.

A Powerful Alternative to Ping: Test-NetConnection

If you like the ping command but want more features, look no further than the PowerShell cmdlet Test-NetConnection. It works as a ping alternative while offering additional capabilities.

Performing a basic ping test

For a simple ping test, use Test-NetConnection followed by a hostname or IP address and set the information level to Quiet. For example, to ping Google.com, enter:

Test-NetConnection Google.com -InformationLevel Quiet

This command returns a Boolean True or False value indicating whether the ping was successful (see Figure 3). To get a more detailed output, remove the -InformationLevel Quiet switch.

Related:How To Fix Network Drive Issues in Elevated PowerShell Sessions

powershell session displays a successful ping to Google.com using the test-netconnection command

Figure 3. The ping to Google.com has succeeded.

Advanced Features of Test-NetConnection

As mentioned above, this tool offers more than basic ping functionality. For example, you can check whether Windows resolves a target host's DNS name, see which DNS is being used, and view the resolved IP addresses.

Examining DNS name resolution

Using the previous example with Google.com, you can examine the DNS name resolution process by entering:

Test-NetConnection | Select-Object NameResolutionSucceeded, ResolvedAddresses, AllNameResolutionResults | Format-List

You can see what this looks like in Figure 4.

powershell session displays the Test-NetConnection tool providing DNS name resolution data

Figure 4. The Test-NetConnection tool has provided DNS name resolution data.

Replacing tracert with Test-NetConnection

The Test-NetConnection cmdlet can replace the tracert tool. Enter the hostname, followed by the -TraceRoute switch. By default, it tests up to 30 hops, but you can adjust this using the -Hops switch. Figure 5 shows a traceroute to Google.com.

powershell session shows the Test-NetConnection tool performing a trace route

Figure 5. The Test-NetConnection tool can perform a traceroute.

Testing communications across specific ports

Of all the features of the Test-NetConnection cmdlet, the one most useful is its ability to test communications across a specific port. Figure 6 illustrates this feature in action.

powershell session shows the Test-NetConnection command testing communications across a specific port

Figure 6. Test-NetConnection can test communications across a specific port.

Related:I’m Struggling to Update My Hybrid IT Environment—Can Powershell Help?

For the sake of demonstration, I set up a Windows Server with the IIS role, allowing the virtual machine to function as a web server. I then configured the Windows firewall to block ICMP traffic, causing a ping test to fail. It is a typical setup for real-world web servers. Additionally, I opened port 80 but blocked port 443. This is the opposite of how the ports would be configured typically, but that's OK.

As shown in Figure 6, the Port 80 test succeeded, while the Port 443 test failed. Moreover, the Port 443 test warned that the ping test failed. Technically, while the ping test for Port 80 also failed, the warning was suppressed because the port communications test succeeded.

Using Test-NetConnection for standard ports

You can also test standard TCP ports without knowing the port number: Enter the Test-NetConnection command followed by the hostname, the -CommonTCPPort switch, and the traffic type you want to test. You can test HTTP, RDP, SMB, and WinRM traffic, as shown in Figure 7.

powershell session demonstrates tests for common traffic types

Figure 7. You can test common traffic types.

PowerShell object output for easier data parsing

Of course, one of the best features of this tool is that it writes its output to a PowerShell object. It allows you to format the output to suit your needs or even include it in the PowerShell pipeline without requiring you to manually parse the data, as you would have to do with the ping command.

Related:How To Create PowerShell Progress Bars for External Tasks (Video Tutorial)

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

https://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like