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:
- Verify Internet connectivity for a host.
- Perform an ICMP ping of a remote host.
- Perform a traceroute to a remote host.
- 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
- Use Test-NetConnection instead of ping.exe and tracert.exe when troubleshooting basic network connectivity.
- Use Test-NetConnection with the -Port parameter for testing connectivity to a remote host’s listening ports.
Reference
- technet.microsoft.com
Mohamed says
Neat,pretty and useful article. keep writing.
Aaron says
Thanks, Mohamed!
vishwajeet says
Very good article and informative too.
Aaron says
Thanks Vishwajeet!
psdude says
Hi Aaron!
Is there somekind of subscription list which I could apply, or how can I assure that I get your latest articles?
Aaron says
It is in the works. I will reply again when it is ready to go. Thanks for your interest!
Aaron Rothstein says
Hey, wanted to let you know the subscription list is active. Thanks for your interest!
Ashok says
Aaron,
Let us know when you ready, would like to subscribe.
A worth reading stuff for a beginner like me, keep them coming..!!
Aaron Rothstein says
Hey, wanted to let you know the subscription list is active. Thanks for your interest!
Neil says
How do you put a timeout in which to get your fail? Trues come fast, and I have to wait a long time for fail, I would like to get through that faster.
Aaron Rothstein says
Unfortunately, Test-NetConnection does not have a parameter letting you adjust the timeouts. That would be a good enhancement.
curtiss says
how do you replace “ping -a” to do a reverse dns lookup on an IP address with test-netconnection?
Aaron Rothstein says
There isn’t a way to do the reverse lookup as part of the Test-NetConnection cmdlet call, but you can use the following to perform a reverse lookup:
$ip = “10.1.1.1”
$hostname = [System.Net.Dns]::GetHostEntry($p).HostName