Testing open ports With PowerShell

We can use PowerShell to test remote port connectivity without installing telnet and with the use of the Test-NetConnection command.

To check if the remote port is open or not we can use the Test-NetConnection command and it requires -The computer name parameter and -Port to check the remote port status.

Method 1

  1. Open PowerShell
  2. Modify the example command below, replacing IP address (or hostname) and port       
Test-NetConnection -ComputerName 192.168.1.1 -Port 443
Test-NetConnection -ComputerName hostname -Port 443

Methord 2
Use the script bellow to check if the port is open.
Create a text File in C:\temp\ with IP.txt. update With the IP address or hostname of the server you are interested in.
Create a text File in C:\temp\ with Ports.txt. update With the portnumber you like to check.

$HOSTFILE = Get-Content "C:\temp\IP.txt"
$PORTFILE = Get-Content "C:\temp\Ports.txt"
foreach ($HOSTLINE in $HOSTFILE)
{
foreach ($PORTLINE in $PORTFILE)
{
$STATUS=(New-Object System.Net.Sockets.TcpClient).ConnectAsync($HOSTLINE, $PORTLINE).Wait(1000)
Write-Output "$HOSTLINE, $PORTLINE, Status: $STATUS"
}
}

Leave a Reply

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