Use PowerShell To Test Port

There are times when we need to identify or troubleshoot if firewall exception is configured correctly and the desired remote server port is open. Often time, I have seen IT Professional use Telnet or PuTTY to test. However, there is another way to do it with PowerShell.

By default, Telnet is not installed on Windows or Windows Server. In order to use Telnet, the Telnet client has to be installed. In Windows, this can be done through Control Panel > Programs and Features > Turn Windows features on or off, and check the Telnet Client. For Windows Server, go to Server Manager > Features > Add Features > Telnet Client.

Or add the Telnet Client feature through PowerShell,
Import-Module ServerManager
Add-WindowsFeature -Name Telnet-Client


Instead of installing the Telnet client, alternatively we could use Windows Sockets through the System.Net.Sockets provided in .NET framework.

To test the TCP port,
$tcp = New-Object System.Net.Sockets.TcpClient
$tcp.connect('<remote server>', <port>)


Or even one line of code if you wish,
(New-Object System.Net.Sockets.TcpClient).Connect('<remote server>', <port>)

That's it! No additional installation (provided that PowerShell is installed, of course). Similar script for UDP protocol using UdpClient.

Here I put them into a PowerShell function (I call it TestPort) for ease of the usage,

function TestPort
{
    Param(
        [parameter(ParameterSetName='ComputerName', Position=0)]
        [string]
        $ComputerName,

        [parameter(ParameterSetName='IP', Position=0)]
        [System.Net.IPAddress]
        $IPAddress,

        [parameter(Mandatory=$true , Position=1)]
        [int]
        $Port,

        [parameter(Mandatory=$true, Position=2)]
        [ValidateSet("TCP", "UDP")]
        [string]
        $Protocol
        )

    $RemoteServer = If ([string]::IsNullOrEmpty($ComputerName)) {$IPAddress} Else {$ComputerName};

    If ($Protocol -eq 'TCP')
    {
        $test = New-Object System.Net.Sockets.TcpClient;
        Try
        {
            Write-Host "Connecting to "$RemoteServer":"$Port" (TCP)..";
            $test.Connect($RemoteServer, $Port);
            Write-Host "Connection successful";
        }
        Catch
        {
            Write-Host "Connection failed";
        }
        Finally
        {
            $test.Dispose();
        }
    }

    If ($Protocol -eq 'UDP')
    {
        Write-Host "UDP port test functionality currently not available."
        <#
        $test = New-Object System.Net.Sockets.UdpClient;
        Try
        {
            Write-Host "Connecting to "$RemoteServer":"$Port" (UDP)..";
            $test.Connect($RemoteServer, $Port);
            Write-Host "Connection successful";
        }
        Catch
        {
            Write-Host "Connection failed";
        }
        Finally
        {
            $test.Dispose();
        }
        #>
    }
}


Some example,
Test Server1 TCP port 1433 (default SQL Server Instance port),
TestPort -ComputerName Server1 -Port 1433 -Protocol TCP

Test IP address 192.168.1.2 UDP port 1434 (SQL Server Browser port),
TestPort -IPAddress 192.168.1.2 -Port 1434 -Protocol UDP

Update: The UDP port test is not functioning correctly. Turns out that UDPClient.connect only specify the host/port to that UDP client without actually connecting the remote host. Since UDP uses connectionless transmission which is hard to reliably determining if a remote UDP port is open/close. it seems like one way to test is to send some packets to the remote UDP port and if "ICMP port unreachable" is received back, the UDP port is considered as close. Otherwise, it is unknown. I have yet to get that concept into the script. I have commented out the UDP section for now.

Return Connection successful if port is open (listening) or else Connection failed

Google+