Last active
January 9, 2025 10:46
-
-
Save pauljnav/1cd511921547e07a7fddfda79473765c to your computer and use it in GitHub Desktop.
Gist an Inactive Tcp Port finder in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-InactiveTcpPort { | |
$listener = [System.Net.Sockets.TcpListener]::new(0) | |
$listener.Start() | |
[int]$port = $listener.LocalEndpoint.Port | |
$listener.Stop() | |
return $port | |
} | |
# faster version | |
function Get-InactiveTcpPort { | |
Begin { | |
$listener = [System.Net.Sockets.TcpListener]::new(0) | |
$listener.Start() | |
} | |
Process { | |
[int]$port = $listener.LocalEndpoint.Port | |
Write-Output $port | |
} | |
End { | |
$listener.Stop() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was inspired to make a small port finder on reading this Gist from the Master StartAutomating Gist a small event-based HTTP server in PowerShell