SoFunction
Updated on 2025-03-09

PowerShell implements test port availability script sharing


Function Test-PortAvailable
{
    param(
    [validaterange(1,65535)]
    [int]$Port
    )
    $sockt=New-Object -ArgumentList 'InterNetwork','Stream','TCP'
    $ip = (Get-NetIPConfiguration).IPv4Address |
        Select -First 1 -ExpandProperty IPAddress
    $ipAddress = []::Parse($ip)
    Try
    {
        $ipEndpoint = New-Object $ipAddress,$port
        $($ipEndpoint)
        return $true
    }
    Catch [exception]
    {
        return $false
    }
    Finally
    {
        $()
    }
}