Site icon Think PowerShell

Use Test-NetConnection to Replace Ping

Test-NetConnection results.

The first post in my PowerShell Beginner series “Daily Tasks, The PowerShell Way“, get started with PowerShell by replacing some classic but obsolete command line tools such as ping.exe, tracert.exe, and more by using Test-NetConnection.

How to start using PowerShell?

There is SO much one can do with PowerShell, often one of the questions is where to start. Why not with tasks you are likely to perform once if not multiple times daily? This is the first post in a PowerShell Beginner series aimed at highlighting tasks that you may currently be doing by GUI or old command line tools and showing how to do them the PowerShell way.

Test-NetConnection: Your new tool for basic network troubleshooting

The first cmdlet we are going to explore is Test-NetConnection. This cmdlet can do four things really well for us:

  1. Verify Internet connectivity for a host.
  2. Perform an ICMP ping of a remote host.
  3. Perform a traceroute to a remote host.
  4. Test a connection to a remote host over a particular port (useful for troubleshooting firewalls).

NOTE: This cmdlet is available in Windows 8.1 / Windows Server 2012 R2 or later. This is only a requirement of the system you are running Test-NetConnection from; the remote host has no requirements.

Test Internet connectivity

Let’s say you are on a computer and you want to verified it’s connected to the Internet. Run Test-NetConnection with no parameters:

PS C:\Users\aaron> Test-NetConnection


ComputerName : internetbeacon.msedge.net
RemoteAddress : 204.79.197.200
InterfaceAlias : Ethernet 2
SourceAddress : 10.1.10.61
PingSucceeded : True
PingReplyDetails (RTT) : 29 ms

You can see it will use a predetermined endpoint operated by Microsoft for verifying Internet connectivity! If you have strict URL filtering in place, this endpoint may not be reachable, but otherwise is a nice quick way of checking Internet connectivity status.

Ping a remote host

When someone is having a problem connecting to a network resource, the first troubleshooting step performed is usually a ping.exe. This ICMP  test can now be performed with Test-NetConnection using the -ComputerName parameter:

PS C:\Users\aaron> Test-NetConnection -ComputerName thinkpowershell.com


ComputerName : thinkpowershell.com
RemoteAddress : 173.236.158.197
InterfaceAlias : Ethernet 2
SourceAddress : 10.1.10.61
PingSucceeded : True
PingReplyDetails (RTT) : 43 ms

Perform a Trace Route

If a ping test fails, you likely will perform a Trace Route to see how far the send attempt gets before it starts failing, and would previously have used the command line tool tracert.exe. Now use Test-NetConnection along with the -ComputerName parameter and -TraceRoute switch:

PS C:\Users\aaron> Test-NetConnection -ComputerName thinkpowershell.com -TraceRoute


ComputerName : thinkpowershell.com
RemoteAddress : 173.236.158.197
InterfaceAlias : Ethernet 2
SourceAddress : 10.1.10.61
PingSucceeded : True
PingReplyDetails (RTT) : 56 ms
TraceRoute : 10.1.10.1
             68.86.94.81
             154.24.15.66
             38.122.62.254
             208.113.156.8
             208.113.156.73
             173.236.158.197

Check if a remote host is listening on a certain port

If a website isn’t reachable on a remote host, or your attempt to Remote Desktop times out, you likely will want to check to see if you can reach the correct listening port on the remote host. A timeout in attempting to reach that port usually indicates the host isn’t listening on that port or a firewall is blocking the traffic attempt. Add the -Port parameter for this level of testing:

PS C:\Users\aaron> Test-NetConnection -ComputerName thinkpowershell.com -Port 443


ComputerName : thinkpowershell.com
RemoteAddress : 173.236.158.197
RemotePort : 443
InterfaceAlias : Ethernet 2
SourceAddress : 10.1.10.61
PingSucceeded : True
PingReplyDetails (RTT) : 44 ms
TcpTestSucceeded : True

Notice that it still performs a ping test to verify the host is up and reachable, then the TCP test of the remote port.

Using Test-NetConnection in a script to test availability and connectivity

If you look at all of the output in the preceding examples, it is human readable. However if you are writing a script and only want to test whether a remote host is up and can listening on a particular port before proceeding with the script, you don’t require all of the additional information. In this case, you can use the -InformationDetail parameter with the value Quiet to simply return $true or $false:

PS C:\Users\aaron> Test-NetConnection -ComputerName thinkpowershell.com -Port 443 -InformationLevel Quiet
True

This can then be put directly into a conditional test such as an If statement:

PS C:\Users\aaron> If (Test-NetConnection -ComputerName thinkpowershell.com -Port 443 -InformationLevel Quiet) {
Write-Host "thinkpowershell.com is listening on port 443."
}
thinkpowershell.com is listening on port 443.

Built-in Alias: TNC

If you are using Test-NetConnection interactively, you can use the built-in alias TNC:

PS C:\Users\aaron> TNC -ComputerName thinkpowershell.com -Port 443 -InformationLevel Quiet
True

Next steps

  1. Use Test-NetConnection instead of ping.exe and tracert.exe when troubleshooting basic network connectivity.
  2. Use Test-NetConnection with the -Port parameter for testing connectivity to a remote host’s listening ports.

Reference

Exit mobile version