• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Short Guides for IT Professionals

MENUMENU
  • HOME
  • ALL TOPICS
    • Active Directory
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Virtualization
    • Web
    • Tools
  • ABOUT
  • SUBSCRIBE
Home » How to Test Open Port in PowerShell

How to Test Open Port in PowerShell

December 4, 2022 - by Zsolt Agoston - last edited on December 4, 2022

There are two ways to test open ports on remote (or the local) hosts in PowerShell.

One is to use the built-in Test-NetConnection cmdlet, the other one is is using a custom function that might be better suited for your needs.

Test-NetConnection

Most of the time this is the command you need. However  the problem with this cmdlet is it pings the remote server before checking the desired port and if ICMP echo (ping) packets are blocked by the remote node, the command will fail even if the port is open on the target.

Examples:

# Test port 443/tcp on host 10.0.0.1
Test-NetConnection -Port 443 -ComputerName 10.0.0.1

# Test port 25/tcp on host mail.protectigate.com
Test-NetConnection -Port 25 -ComputerName mail.protectigate.com

Test port with custom function

Use this following function as a fast and reliable port checker tool:

Function Test-Port {
	Param (
		[string]$Server,
		[int]$Port
	)

	Try {
		$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $Server, $Port
		Write-Host -ForegroundColor green "[Open]`n"
	}
	Catch {
		Write-Host -ForegroundColor magenta "[Closed]`n"
	}
}

Reader Interactions

Community Questions Cancel reply

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

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • Test-NetConnection
  • Examples:
  • Test port with custom function

  • Terms of Use
  • Disclaimer
  • Privacy Policy