Check a TCP or UDP Port with PowerShell (Test-NetConnection)

Advertisement
Check a TCP or UDP Port with PowerShell (Test-NetConnection) Network
Advertisement

When Remote Desktop, a NAS or a server won’t connect, the first question is whether your traffic even reaches the port on the other side. PowerShell can answer that on its own.

Short answer: for a TCP port, run Test-NetConnection with -Port. If TcpTestSucceeded is True, the port is reachable. For a UDP port, Test-NetConnection can’t help. Check that the other side is listening, then send real data and see if anything comes back.

Test-NetConnection -ComputerName www.google.com -Port 443

All examples were run on Windows 11 with Windows PowerShell 5.1. I replaced my IP addresses with placeholders like <your PC IP>.

Check a TCP port: Test-NetConnection -Port

Put the host name or IP address in -ComputerName and the port number in -Port. Against Google on port 443 (HTTPS), I got this:

ComputerName     : www.google.com
RemoteAddress    : 142.251.152.119
RemotePort       : 443
InterfaceAlias   : Ethernet
SourceAddress    : <your PC IP>
TcpTestSucceeded : True

The line to look at is TcpTestSucceeded. True means a TCP connection to that port worked. The other fields:

Field Meaning
ComputerName The target you gave
RemoteAddress The target IP address after name resolution
RemotePort The port that was tested
InterfaceAlias The network adapter your PC used
SourceAddress Your PC’s IP address on that adapter
TcpTestSucceeded Whether the TCP connection worked (True / False)

I also tried my NAS on port 445 (SMB file sharing):

Test-NetConnection -ComputerName <NAS> -Port 445
ComputerName     : <NAS>
RemoteAddress    : <NAS IP>
RemotePort       : 445
InterfaceAlias   : Ethernet
SourceAddress    : <your PC IP>
TcpTestSucceeded : True

If a NAS share won’t open but port 445 is True, the network is fine. The problem is on the sign-in or SMB settings side (on Windows 11 24H2 and later, see Windows 11 24H2 can’t access a NAS: guest access and SMB signing).

Common ports by name

-CommonTCPPort takes a name instead of a number: HTTP, RDP, SMB or WINRM. To check Remote Desktop (port 3389) on your own PC:

Test-NetConnection -ComputerName localhost -CommonTCPPort RDP
ComputerName     : localhost
RemoteAddress    : ::1
RemotePort       : 3389
InterfaceAlias   : Loopback Pseudo-Interface 1
SourceAddress    : ::1
TcpTestSucceeded : True

What a failure looks like, and why it’s slow

Port 81 on Google isn’t in use. Testing it gave this:

Test-NetConnection -ComputerName www.google.com -Port 81
TCP connect to (142.251.152.119 : 81) failed
TCP connect to (142.251.154.119 : 81) failed
TCP connect to (142.251.153.119 : 81) failed
TCP connect to (142.251.156.119 : 81) failed
TCP connect to (142.251.157.119 : 81) failed
TCP connect to (142.251.150.119 : 81) failed
TCP connect to (142.251.155.119 : 81) failed
TCP connect to (142.251.151.119 : 81) failed
TCP connect to (2001:4860:482c:7700:: : 81) failed
TCP connect to (2001:4860:482b:7700:: : 81) failed
TCP connect to (2001:4860:4828:7700:: : 81) failed
TCP connect to (2001:4860:4826:7700:: : 81) failed
TCP connect to (2001:4860:482a:7700:: : 81) failed
TCP connect to (2001:4860:4829:7700:: : 81) failed
TCP connect to (2001:4860:4827:7700:: : 81) failed
TCP connect to (2001:4860:482d:7700:: : 81) failed

ComputerName           : www.google.com
RemoteAddress          : 142.251.152.119
RemotePort             : 81
InterfaceAlias         : Ethernet
SourceAddress          : <your PC IP>
PingSucceeded          : True
PingReplyDetails (RTT) : 2 ms
TcpTestSucceeded       : False

TcpTestSucceeded is False. When the port test fails, you also get PingSucceeded. If ping is True but TCP is False, the host is reachable but that port isn’t. Many devices don’t answer ping, though, so a False ping doesn’t prove the host is unreachable.

Watch the time: this one run took about 169 seconds. The TCP connect to ... failed lines show why: www.google.com resolved to 16 addresses (8 IPv4, 8 IPv6), and it tried every one in turn. For a name with many addresses, a failure keeps you waiting a long time. If you’re in a hurry, use the short-timeout check further down.

A wrong host name that can’t be resolved fails right away. There’s no TCP result, and RemoteAddress is empty:

Name resolution of no-such-host.invalid failed

ComputerName   : no-such-host.invalid
RemoteAddress  : 
InterfaceAlias : 
SourceAddress  : 
PingSucceeded  : False

When it says False, suspect these

  • The service that uses the port isn’t running on the other side (see “Is the other side listening?” below)
  • Windows Firewall (or another firewall) on the other side blocks incoming traffic on that port
  • A router, firewall or VPN in between blocks it
  • It’s actually a UDP port (Test-NetConnection only tests TCP)

Only True or False: -InformationLevel Quiet

In a script, add -InformationLevel Quiet and the result is just True or False.

Test-NetConnection -ComputerName www.google.com -Port 443 -InformationLevel Quiet

Quiet isn’t faster: port 81 still took about 169 seconds.

To see more, such as the name resolution results (NameResolutionResults) and the next router (NetRoute (NextHop)), use -InformationLevel Detailed.

Test-NetConnection -ComputerName www.google.com -Port 443 -InformationLevel Detailed

Check with a short timeout (and several ports at once)

Test-NetConnection has no timeout option. To avoid the long waits, try the connection with .NET’s TcpClient and give up after a set time. This function gives up after 1 second (1000 ms):

function Test-TcpPort($ComputerName, $Port, $TimeoutMs = 1000) {
    $client = New-Object System.Net.Sockets.TcpClient
    try {
        $client.ConnectAsync($ComputerName, $Port).Wait($TimeoutMs)
    } catch {
        $false
    } finally {
        $client.Dispose()
    }
}

Test-TcpPort www.google.com 443
Test-TcpPort www.google.com 81

It returns True if the connection works in time, False if not. On my PC, port 443 was True and port 81 was False after 1 second, instead of about 169 seconds with Test-NetConnection.

To check several ports, load the function above and run:

foreach ($p in 80, 443, 81) {
    [pscustomobject]@{ Port = $p; Open = (Test-TcpPort www.google.com $p) }
}
Port  Open
----  ----
  80  True
 443  True
  81 False

On a slow line 1 second may be too short, so if you get False, try again with $TimeoutMs set to 3000 or so.

Is the other side listening? Get-NetTCPConnection

If the other machine runs Windows and you can use it, check there whether anything is listening on the port. If nothing shows up, the cause is a service that isn’t running, not the network. For Remote Desktop on port 3389:

Get-NetTCPConnection -LocalPort 3389 -State Listen
LocalAddress                        LocalPort RemoteAddress                       RemotePort State       AppliedSetting OwningProcess 
------------                        --------- -------------                       ---------- -----       -------------- ------------- 
::                                  3389      ::                                  0          Listen                     2204          
0.0.0.0                             3389      0.0.0.0                             0          Listen                     2204          

A LocalAddress of 0.0.0.0 (IPv4) or :: (IPv6) means it listens on every address of the PC. If it shows only 127.0.0.1, only the PC itself can connect.

To see which program is listening, turn OwningProcess (the process ID) into a process name:

Get-NetTCPConnection -LocalPort 3389 -State Listen | Select-Object LocalAddress, LocalPort, State, OwningProcess, @{n='ProcessName';e={(Get-Process -Id $_.OwningProcess).ProcessName}}

On my PC it showed svchost, because Remote Desktop runs as a Windows service. This worked without an administrator PowerShell.

Check a UDP port

UDP has no connection, so there’s no “did it connect?” to look at. You only know a packet arrived when the other side sends something back. So check in this order.

Step 1: Is a program listening? (Get-NetUDPEndpoint)

On the Windows machine on the other side, check whether a program has that UDP port open:

Get-NetUDPEndpoint -LocalPort 3389
LocalAddress                             LocalPort      
------------                             ---------      
::                                       3389           
0.0.0.0                                  3389           

Remote Desktop uses UDP 3389 as well as TCP. Tailscale’s default UDP port 41641 was open on my PC too:

Get-NetUDPEndpoint -LocalPort 41641

If it shows up, a program has the port open. That still doesn’t tell you whether packets from your PC get there.

Step 2: Ask the service itself (DNS example)

If you can send the service a request it answers, that’s the surest test. For DNS (UDP 53), point Resolve-DnsName at the server:

Resolve-DnsName -Name www.google.com -Server 8.8.8.8 -DnsOnly -Type A
Name                                           Type   TTL   Section    IPAddress                                
----                                           ----   ---   -------    ---------                                
www.google.com                                 A      186   Answer     142.251.150.119                          
www.google.com                                 A      186   Answer     142.251.154.119                          
(more lines)

An answer means your query reached UDP 53 on 8.8.8.8 and the reply came back. Against an address with no DNS server (192.0.2.1, reserved for documentation), it failed after about 7 seconds with:

Resolve-DnsName : www.google.com : This operation returned because the timeout period expired

For other services (SNMP and so on), the same idea applies: use that service’s own command or tool and see if it answers.

Step 3: Send and receive with PowerShell (when you can use both PCs)

If there’s no way to query the service, run a small receiver in PowerShell on the other side and send to it from yours. The receiver echoes back what it gets, so a reply proves the packet made it both ways.

On the other side (the receiver), run this. 50000 is just an example, so change it to the port you want to test (you can’t listen on a port a running service already uses).

$port = 50000
$udp = New-Object System.Net.Sockets.UdpClient $port
$udp.Client.ReceiveTimeout = 60000
$remote = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Any, 0)
$data = $udp.Receive([ref]$remote)
$udp.Send($data, $data.Length, $remote) | Out-Null
$udp.Close()
"Received: " + [Text.Encoding]::UTF8.GetString($data) + " from " + $remote

Within the 60 seconds (60000 ms) it listens, run this on your side (the sender). Put the other PC’s IP address in $target.

$target = "192.168.1.10"
$port = 50000
$udp = New-Object System.Net.Sockets.UdpClient
$udp.Client.ReceiveTimeout = 3000
$bytes = [Text.Encoding]::UTF8.GetBytes("hello")
$udp.Send($bytes, $bytes.Length, $target, $port) | Out-Null
$remote = New-Object System.Net.IPEndPoint ([System.Net.IPAddress]::Any, 0)
try {
    "Reply: " + [Text.Encoding]::UTF8.GetString($udp.Receive([ref]$remote))
} catch {
    "No reply: " + $_.Exception.Message
} finally {
    $udp.Close()
}

If the sender prints Reply: hello, it got through (I tested both ends on one PC). If not, check whether Windows Firewall on the receiving side blocks it. If you send with no receiver running, the sender prints:

No reply: Exception calling "Receive" with "1" argument(s): "An existing connection was forcibly closed by the remote host"

It says the connection was “forcibly closed”, but what it really means is that the other side replied “no program is listening on that port”.

Summary

What you want to know Command
Is a TCP port reachable? Test-NetConnection -ComputerName target -Port number
Just True / False Add -InformationLevel Quiet
No long waits TcpClient with a timeout (Test-TcpPort in this post)
Is TCP listening on the other side? Get-NetTCPConnection -LocalPort number -State Listen
Is UDP open on the other side? Get-NetUDPEndpoint -LocalPort number
Does UDP get through? Query the service (Resolve-DnsName -Server for DNS), or send and receive with PowerShell

Test-NetConnection only tests TCP. Don’t read a False on a UDP port as “can’t connect”.

Related posts

References

Comments

Copied title and URL