olly - Fotolia

Manage Learn to apply best practices and optimize your operations.

Try these PowerShell networking commands to stay connected

Ping has its place, but PowerShell gives you a way to dive deeper into a networking issue when a server drops its connection or starts to drop packets.

While it would be nice if they did, servers don't magically stay online on their own.

Servers go offline for a lot of reasons; it's your job to find a way to determine network connectivity to these servers quickly and easily. You can use PowerShell networking commands, such as the Test-Connection and Test-NetConnection cmdlets to help.

The problem with ping

For quite some time, system administrators used ping to test network connectivity. This little utility sends an Internet Control Message Protocol message request to an endpoint and listens for an ICMP reply.

ping test
The ping utility runs a fairly simple test to check for a response from a host.

Because ping only tests ICMP, this limits its effectiveness to fully test a connection. Another caveat: The Windows firewall blocks ICMP requests by default. If the ICMP request doesn't reach the server in question, you'll get a false negative which makes ping results irrelevant.

The Test-Connection cmdlet offers a deeper look

We need a better way to test server network connectivity, so let's use PowerShell instead of ping. The Test-Connection cmdlet also sends ICMP packets but it uses Windows Management Instrumentation which gives us more granular results. While ping returns text-based output, the Test-Connection cmdlet returns a Win32_PingStatus object which contains a lot of useful information.

The Test-Connection command has a few different parameters you can use to tailor your query to your liking, such as changing the buffer size and defining the number of seconds between the pings. The output is the same but the request is a little different.

Test-Connection www.google.com -Count 2 -BufferSize 128 -Delay 3

You can use Test-Connection to check on remote computers and ping a remote computer as well, provided you have access to those machines. The command below connects to the SRV1 and SRV2 computers and sends ICMP requests from those computers to www.google.com:

Test-Connection -Source 'SRV2', 'SRV1' -ComputerName 'www.google.com'

Source   Destination     IPV4Address      IPV6Address
Bytes    Time(ms)

------   -----------     -----------      -----------
-----    --------

SRV2    google.com      172.217.7.174
32       5

SRV2    google.com      172.217.7.174
32       5

SRV2    google.com      172.217.7.174
32       6

SRV2    google.com      172.217.7.174
32       5

SRV1    google.com      172.217.7.174
32       5

SRV1    google.com      172.217.7.174
32       5

SRV1    google.com      172.217.7.174
32       5

SRV1    google.com      172.217.7.174
32       5

If the output is too verbose, and you just want a simple result, use the Quiet parameter.

Test-Connection -ComputerName google.com -Quiet
True

For more advanced network checks, try the Test-NetConnection cmdlet

If simple ICMP requests aren't enough to test network connectivity, PowerShell also provides the Test-NetConnection cmdlet. This cmdlet is the successor to Test-Connection and goes beyond ICMP to check network connectivity.

For basic use, Test-NetConnection just needs a value for the ComputerName parameter and will mimic Test-Connection's behavior.

Test-NetConnection -ComputerName www.google.com


ComputerName           : www.google.com
RemoteAddress          : 172.217.9.68
InterfaceAlias         : Ethernet 2
SourceAddress          : X.X.X.X
PingSucceeded          : True
PingReplyDetails (RTT) : 34 ms

Test-NetConnection has advanced capabilities and can test for open ports. The example below will check to see if port 80 is open:

Test-NetConnection -ComputerName www.google.com -Port 80

ComputerName     : google.com
RemoteAddress    : 172.217.5.238
RemotePort       : 80
InterfaceAlias   : Ethernet 2
SourceAddress    : X.X.X.X
TcpTestSucceeded : True

The boolean TcpTestSucceeded returns True to indicate port 80 is open.

We can also use the TraceRoute parameter with the Test-NetConnection cmdlet to check the progress of packets to the destination address.

Test-NetConnection -ComputerName google.com -TraceRoute


ComputerName           : google.com
RemoteAddress          : 172.217.5.238
InterfaceAlias         : Ethernet 2
SourceAddress          : X.X.X.X
PingSucceeded          : True
PingReplyDetails (RTT) : 44 ms
TraceRoute             : 192.168.86.1
                         192.168.0.1
                         142.254.146.117
                         74.128.4.113
                         65.29.30.36
                         65.189.140.166
                         66.109.6.66
                         66.109.6.30
                         107.14.17.204
                         216.6.87.149
                         72.14.198.28
                         108.170.240.97
                         216.239.54.125
                         172.217.5.238

If you dig into the help for the Test-NetConnection cmdlet, you'll find it has quite a few parameters to test many different situations.

Dig Deeper on Windows administration tools

Join the conversation

1 comment

Send me notifications when other members comment.

Please create a username to comment.

What are some network troubleshooting features in PowerShell that you use?
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close