Cyber Security

PowerShell: Test open TCP Ports with Test-OpenPort (multiple hosts, multiple port numbers)

As the headline says, it’s all about port scanning today. When a computer sends SYN to another computer, the remote computer will usually answer with SYN + ACK or RST. By this fact, we can test if a port is open or not.



Different port scanners can be used to test whether a port is open or not. Or Test-NetConnection or a self-created script. Remember Test NetConnection:


But if you try to test multiple computers and multiple ports you are faced with an error message.


The following function addresses this issue. Test-OpenPort allows testing multiple computers and multiple ports at once.


Before we get to the function itself, I would like to show my function in action. The function has two parameters: Target and Port.

Test-OpenPort -Target -Port 80


These parameters are not named, so therefore you can omit the parameter names.

Test-OpenPort 80


You can also omit the Target. The default value for this parameter is localhost.

Test-OpenPort -Port 80,443


Running on multiple computers and ports requires the use of a comma.

Test-OpenPort, -Port 80,443


The screen’s output is an object. Therefore you are able to customize this object with Select-Object or the Format-Commands (Format-Table …)

Test-OpenPort, -Port 80,443,53 | Sort-Object Status


For a more sweeter view:

Test-OpenPort, -Port 80,443,53 | Sort-Object Status | Out-GridView


The Function

Copy this function into your PowerShell ISE session and press the green start button.

function Test-OpenPort {


Test-OpenPort is an advanced Powershell function. Test-OpenPort acts like a port scanner. 

Uses Test-NetConnection. Define multiple targets and multiple ports. 

Define the target by hostname or IP-Address. Separate them by comma. Default: localhost 

Mandatory. Define the TCP port. Separate them by comma. 

Test-OpenPort -Target,, -Port 80,443 

Author: Patrick Gruenauer









[Parameter(Mandatory=$true, Position=1, Helpmessage = 'Enter Port Numbers. Separate them by comma.')]



foreach ($t in $Target)


foreach ($p in $Port)


$a=Test-NetConnection -ComputerName $t -Port $p -WarningAction SilentlyContinue

$result+=New-Object -TypeName PSObject -Property ([ordered]@{




Write-Output $result


Make it permanent

If you like my approach open PowerShell ISE. Copy the function into your ISE session. Create a folder in C:\Program Files\Windows PowerShell\Modules and save the code as psm1 file. Make sure that your file name and folder name match.



Close PowerShell. Open PowerShell again. The command is now available for all users. Have fun with Test-OpenPort!

13 replies »

  1. PS> $pcname =
    PS> $ports = 135, 443, 445, 3389, 3268
    PS> foreach ($p in $ports) {Test-NetConnection -port $p -computername $pcname}


  2. For someone who wants to test a port on multiple server and server list is in a txt file

    PS C:\> $server = Get-Content -path “C:\ServerList.txt”

    PS C:\> Test-OpenPort -Target $server -Port 443 | Sort-Object Status | Out-GridView


  3. Patrick Gruenauer you are awesome. Thanks a lot.

    Is there a way we can reduce the PingReplyDetails (RTT) from 6 ms to something lesser?


  4. I’m trying to prompt for the server names and the ports to scan and pass those variables into your function but not having any luck.
    below i have the two variables of the server names prompt and the ports prompt, then passing into your function call.
    I am not extremely proficient in powershell but tinker with it, and this would be super helpful if this is possible.
    Can you advise if this can be done?

    $servernames = Read-Host -Prompt “Enter Server Names or IPs separated by commas – no spaces (Then press ENTER)”

    $ports = Read-Host -Prompt “Enter Port Numbers Separated by Commas – no spaces (Then press ENTER)”

    Test-OpenPort -Target $servernames -Port $ports

    your function is below no changes made


    • Tried it out. The problem is that Read-Host saves your input as a string.

      Try to separate the values with the split method as shown below and then it works!

      PS C:\> $servers,
      PS C:\> $server1=$servers.split(‘,’)
      PS C:\> $server1
      PS C:\> Test-OpenPort -Target $server1 -Port 443

      Target RemoteAddress Port Status
      —— ————- —- —— 443 True 443 True


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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