Config Server Firewall

How to Traceroute in PowerShell

In Windows CMD, we can use either the tracert or pathping commands to perform a traceroute. These two commands are also available in PowerShell; however, PowerShell has its own method for performing traceroutes.

Related Commands

In PowerShell, you can perform a traceroute with either Test-Connection or Test-NetConnection. While Test-NetConnection offers superior speed, Test-Connection provides more detailed information.

Using Test-Connection to Traceroute

Here's the command syntax in PowerShell for using Test-Connection to perform a traceroute:

Test-Connection -TargetName <Computer-Name/IP-Address> -Traceroute

Specify the target to trace with the -TargetName parameter. This can be either the IP address or the domain name of the remote computer. For example, the following command traces the example.local domain name:

Test-Connection -TargetName example.local -Traceroute

The result will be a detailed table showing the route taken by the packets:

How to Traceroute in PowerShell
Traceroute using the Test-Connection command.

Each hop is listed three times as Test-Connection sends three echo request packets to each device for reliable connection verification.

It is also possible to traceroute more than one address at once. Simply separate the addresses with commas. For example, the following command traces the route to IP addresses 192.168.1.1 and 192.168.1.10 at once:

Test-Connection -TargetName 192.168.1.1, 192.168.1.10 -Traceroute

While tracert automatically resolves hostnames, Test-Connection doesn't by default. However, you can enable name resolution by adding the -ResolveDestination switch.

Test-Connection -TargetName example.local -Traceroute -ResolveDestination

You can also change the output format by redirecting the output to the Format-List command:

Test-Connection -TargetName 192.168.1.1 | Format-List

Using Test-NetConnection to Traceroute

To perform a traceroute using the Test-NetConnection command, use the following command syntax:

Test-NetConnection -ComputerName <Computer-Name/IP-Address> -TraceRoute

This example maps the network path to the example.local domain using Test-NetConnection:

Test-NetConnection -ComputerName example.local -TraceRoute

You will see an output similar to the following, where Test-NetConnection lists all the hops it traverses on its way to reach the remote server.

Traceroute using the Test-NetConnection command.
Traceroute using the Test-NetConnection command.

Not only is the response significantly faster than Test-Connection, but it also shows the IP address of the local interface that data is flowing out from. This can be very useful if your computer has multiple network interfaces.