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" } }
Comments