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 ny-msg01.woshub.com 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

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

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: