Posted on December 2, 2016 · Posted in Powershell

Checking TCP Port Response Using PowerShell

From time to time, any administrator has to check service availability on a remote server  by testing remote TCP port response. Meanwhile, everybody got used to doing it quickly using telnet command. For example, to make sure the SMTP service is response on a mail server (by default, it responds at TCP Port 25) it is enough to run telnet 25 command. But starting from Windows 7, the telnet client has become a feature to be installed separately. Let’s see how to check for open TCP port using PowerShell.

A ready-to-use cmdlet to check network connection — Test-NetConnection – has appeared in PowerShell 4.0 (Windows 2012 R2, Windows 8.1or higher). It is a part of NetTCPIP module.

Tip. You can check what PowerShell version you have got using the command $PSVersionTable.PSVersion


Value 4 in the Major column means that PowerShell 4.0 is installed on your computer.

Let’s check if TCP Port 25 (SMTP protocol) is available on the mail server using Test-NetConnection:

Test-NetConnection -ComputerName ny-msg01 -Port 25

Note. Using Test-NetConnection cmdlet, you can check only TCP connection, and it is not applicable to check the availability of the remote  UDP port.

The shortened version of the same command looks like this:
TNC ny-msg01 -Port 25

Test-NetConnection check remote tcp port

Let’s consider the result of the command:

ComputerName           : ny-msg01
RemoteAddress          :
RemotePort             : 25
InterfaceAlias         : CORP
SourceAddress          :
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : True

As we can see, the specified server responds via ICMP (PingSucceeded = True) and TCP Port 25 also responds (RemotePort=25, TcpTestSucceeded= True)

Note. In some cases, it may occur that PingSucceeded=False, and TcpTestSucceeded=True. It is likely to mean that ICMP Ping is forbidden on remote server.

If you only need to see if the port is available, it can be checked in a shorter way:

TNC ny-msg1 -Port 25 -InformationLevel Quiet

TNC ny-msg1 -Port 25 -InformationLevel Quiet

The following command allows to check the availability of a specific port on a number of servers, the list of which is stored in a plain text file list_servers.txt. We need the servers without the response from the specific service:

Get-Content c:\PS\list_servers.txt |  where { -NOT (Test-Netconnection $_ -Port 25  -InformationLevel Quiet)}| Format-Table -AutoSize

Tip. In earlier PowerShell versions, you could check TCP port availability as follows:

(New-Object System.Net.Sockets.TcpClient).Connect(‘ny-msg01’, 25)

(New-Object System.Net.Sockets.TcpClient).Connect

Related Articles