Skip to main content
Powershell

Test-NetConnection: Check for Open/Closed Ports from PowerShell

By January 6, 2020One Comment

Test-NetConnection – a ready-to-use cmdlet to check network connection has appeared in PowerShell 4.0 (Windows 2012 R2, Windows 8.1 and newer). You can use this cmdlet to check the response and availability of a remote server or network service on it, TCP ports blocked by firewalls, check ICMP availability and routing. In fact, the Test-NetConnection cmdlet can replace several standard network admin tools at once: ping, traceroute, TCP port scanner, etc.

From time to time, any administrator has to check service availability on a remote server by checking remote TCP port response (for example, the availability of an email or web server). Moreover, most admins are used to perform such a port check with the telnet command. For example, to make sure the SMTP service  responds on the email server (by default, it responds on TCP Port 465) it is enough to run telnet smtp.gmail.com 465 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/closed TCP ports using PowerShell.

The main benefit of the Test-NetConnection cmdlet is that it is already a part of all modern versions of Windows and you don’t need to install it separately. The cmdlet is a part of the NetTCPIP module (starting with PoSh v4.0).

Tip. You can check the current installed version of PowerShell with the command: $PSVersionTable.PSVersion

$PSVersionTable.PSVersion

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

Testing for Open/Closed Server TCP Ports with Test-NetConnection

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

Test-NetConnection -ComputerName smtp.gmail.com -Port 465

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

The shortened version of the same command looks like this: TNC smtp.gmail.com -Port 465

Test-NetConnection check remote tcp port

Let’s consider the result of the command:

ComputerName           : smtp.gmail.com
RemoteAddress          : 173.194.68.108
RemotePort             : 465
InterfaceAlias         : Ethernet
SourceAddress          : 10.20.1.79
PingSucceeded          : True
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : True

As you can see, the cmdlet resolves the server name to IP address, checks the ICMP response (similar to ping) and the availability of the TCP port. The specified server responds via ICMP (PingSucceeded = True) and the TCP Port 465 is open (RemotePort=465, 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 the remote server.

The cmdlet has a special parameter –CommonTCPPort, which allows you to specify the name of a known network protocol (HTTP, RDP, SMB, WINRM).

For example, to check the availability of an HTTP web server, you can use the command:

Test-NetConnection -ComputerName zamarax.com -CommonTCPPort HTTP

Or RDP port (3389) availability:

Test-NetConnection MyRDSServer.contoso.com –CommonTCPPort RDP

You can list all the parameters that the Test-NetConnection cmdlet returns:

Test-NetConnection smtp.gmail.com -port 465 | Format-List *

Test-NetConnection all connection state properties

If you only need to see if the port is available, it can be checked more quickly:

TNC smtp.gmail.com -Port 465 -InformationLevel Quiet

The cmdlet returned True, which means the remote port is accessible.

TNC ny-msg1 -Port 25 -InformationLevel Quiet

TipIn earlier PowerShell versions, you could check TCP port availability as follows:

(New-Object System.Net.Sockets.TcpClient).Connect(‘smtp.gmail.com’, 465)

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

In Windows 10 / Windows Server 2016, you can use the Test-NetConnection cmdlet to trace the route to a remote server using the –TraceRoute parameter (analogous to tracert command in Windows). Using the –Hops parameter, you can limit the maximum number of hopes during route check.

Test-NetConnection smtp.gmail.com –TraceRoute

The cmdlet returned the network summary delay when accessing the server in milliseconds (PingReplyDetails (RTT): 41 ms) and all the IP addresses of the routers on the way to the target server.

Test-NetConnection: powershell TraceRoute

Test-NetConnection in PowerShell Monitoring Scripts

The following command allows you 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 where the specified service doesn’t respond:

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

Similarly, you can create a simple monitoring script that checks the availability of servers and displays a notification if one of the servers is unavailable.

For example, you can check the availability of basic services on all domain controllers (a DC list can be obtained with the Get-ADDomainController cmdlet). Let’s check the following services on DC (the PortQry tool has a similar “Domain and trusts” rule):

  • RPC – TCP/135
  • LDAP – TCP/389
  • LDAP – TCP/3268
  • DNS – TCP/53
  • Kerberos – TCP/88
  • SMB – TCP/445

$Ports  = "135","389","636","3268","53","88","445","3269", "80", "443"
$AllDCs = Get-ADDomainController -Filter * | Select-Object Hostname,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem
ForEach($DC in $AllDCs)
{
Foreach ($P in $Ports){
$check=Test-NetConnection $DC -Port $P -WarningAction SilentlyContinue
If ($check.tcpTestSucceeded -eq $true)
{Write-Host $DC.name $P -ForegroundColor Green -Separator " => "}
else
{Write-Host $DC.name $P -Separator " => " -ForegroundColor Red}
}

The script will check the specified TCP ports on the domain controllers, and if one of the ports is unavailable, it will highlight it in red (you can run this PowerShell script as a Windows service).

poweshell: test for open and closed ports on an active directory domain controller

Simple IP Network / Port Scanner with PowerShell

You can also implement a simple port and IP subnet network scanner to scan remote servers or subnets for open/closed TCP ports.

Scan the range of IP addresses on open port 3389:

foreach ($ip in 100..150) {Test-NetConnection -Port 3389 -InformationLevel "Detailed" 192.168.1.$ip}

Scan the range of TCP ports from 1 to 1024 on the specified remote server:

foreach ($port in 1..1024) {If (($a=Test-NetConnection srvfs01 -Port $port -WarningAction SilentlyContinue).tcpTestSucceeded -eq $true){ "TCP port $port is open!"}}

powershell network port scanner script

One Comment

  • 52 DevOps says:

    $Ports = “135”,”389″,”636″,”3268″,”53″,”88″,”445″,”3269″,”80″,”443″;
    $AllDCs = Get-ADDomainController -Filter * | Select-Object hostname,name,Ipv4address,isGlobalCatalog,Site,Forest,OperatingSystem;
    ForEach($DC in $AllDCs){
    Foreach ($P in $Ports){
    $check=Test-NetConnection -ComputerName $DC.name -Port $P -WarningAction SilentlyContinue
    If ($check.tcpTestSucceeded -eq $true){Write-Host $DC.name $P -ForegroundColor Green -Separator ” => “}
    else
    {Write-Host $DC.name $P -Separator ” => ” -ForegroundColor Red}}}

    The script above works perfectly.

Leave a Reply