PowerShell Script to Check if Port is Open
Test-NetConnection -ComputerName 'hostname' -Port portnumber in PowerShell to check if a port is open; it returns connection status and details.Examples
How to Think About It
Test-NetConnection command does this by attempting a TCP connection and reporting the result.Algorithm
Code
param(
[string]$ComputerName = 'localhost',
[int]$Port = 80
)
$result = Test-NetConnection -ComputerName $ComputerName -Port $Port
if ($result.TcpTestSucceeded) {
Write-Output "Port $Port on $ComputerName is open."
} else {
Write-Output "Port $Port on $ComputerName is closed or unreachable."
}Dry Run
Let's trace checking port 80 on localhost through the code
Set parameters
$ComputerName = 'localhost', $Port = 80
Run Test-NetConnection
Test-NetConnection tries to connect to localhost on port 80
Check result
$result.TcpTestSucceeded is True
Output message
Print 'Port 80 on localhost is open.'
| Step | Action | Value |
|---|---|---|
| 1 | Parameters | localhost, 80 |
| 2 | Test-NetConnection result | TcpTestSucceeded = True |
| 3 | Output | Port 80 on localhost is open. |
Why This Works
Step 1: Using Test-NetConnection
The Test-NetConnection cmdlet tries to open a TCP connection to the specified computer and port.
Step 2: Checking TcpTestSucceeded
The property TcpTestSucceeded tells if the connection was successful, meaning the port is open.
Step 3: Outputting result
Based on the connection success, the script prints whether the port is open or closed.
Alternative Approaches
param(
[string]$ComputerName = 'localhost',
[int]$Port = 80
)
$tcpClient = New-Object System.Net.Sockets.TcpClient
try {
$tcpClient.Connect($ComputerName, $Port)
Write-Output "Port $Port on $ComputerName is open."
} catch {
Write-Output "Port $Port on $ComputerName is closed or unreachable."
} finally {
$tcpClient.Dispose()
}param(
[string]$ComputerName = 'localhost',
[int]$Port = 80
)
if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) {
$socket = New-Object System.Net.Sockets.TcpClient
try {
$socket.Connect($ComputerName, $Port)
Write-Output "Port $Port on $ComputerName is open."
} catch {
Write-Output "Port $Port on $ComputerName is closed or unreachable."
} finally {
$socket.Dispose()
}
} else {
Write-Output "$ComputerName is unreachable."
}Complexity: O(1) time, O(1) space
Time Complexity
The operation is a single network connection attempt, so it runs in constant time regardless of input size.
Space Complexity
The script uses a fixed amount of memory for variables and connection objects, so space is constant.
Which Approach is Fastest?
Using Test-NetConnection is fastest and simplest because it is optimized and built-in; using TcpClient is slightly more manual and verbose.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Test-NetConnection | O(1) | O(1) | Simple, modern PowerShell scripts |
| System.Net.Sockets.TcpClient | O(1) | O(1) | Older PowerShell versions or custom handling |
| Test-Connection + TcpClient | O(1) | O(1) | When host reachability must be confirmed first |
Test-NetConnection for a simple and reliable port check in PowerShell 4.0 and above.