Testing if a Port is Open With PowerShell

Ever want to confirm that a port is accessible from one computer to another? There’s a PowerShell cmdlet for that, Test-NetConnection. With the -Port option, this cmdlet will do a quick TCP three-way handshake on the remote system to confirm that the service is available and reports back if it succeeded or not. Check out that last line of output TcpTestSucceeded: False. That indicates that this port is not accessible. You can see, however, that the system is reachable via ICMP (Ping), PingSuceeded: True so we know that the remote system is alive, just not listening on the port we want to access.

This cmdlet is from Windows PowerShell 5.1

PS C:\Users\demo\Documents> Test-NetConnection server1.centinosytems.lab -Port 3389
WARNING: TCP connect to server1.centinosytems.lab:3389 failed

ComputerName : server1.centinosytems.lab
RemoteAddress : 172.16.94.10
RemotePort : 3389
InterfaceAlias : Ethernet0
SourceAddress : 172.16.94.11
PingSucceeded : True
PingReplyDetails (RTT) : 1 ms
TcpTestSucceeded : False 

In PowerShell Core, there’s a Test-Connection which in version 6.0 supports basic ICMP ECHO functionality.  In version 6.1, a -TcpPort option was added for testing connectivity to a port. The current implementation of the v6 cmdlet returns a Boolean based on the reachability of the port.

If you really want to get under the hood and learn how networking…works. Check out my LFCE: Advanced Linux Networking course. In this course, we cover IP addressing, routing, TCP internals and troubleshooting. We use the Linux command line, not PowerShell, but it really shows you how things move around a network. It’s one of my most popular courses.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.