Test-NetConnection: Test TCP Ports and Diagnose Networks

A laptop, smartphone, and Wi-Fi router connected in a workspace.

Written by

in

PowerShell ships with a built-in answer to “can this machine reach that service?” You run Test-NetConnection with a hostname and a port, and it reports name resolution, the interface it used, your source address, and whether the TCP handshake completed. For Windows admins, it replaces the old habit of chaining ping, telnet, tracert, and nslookup.

A laptop, smartphone, and Wi-Fi router connected in a workspace.

To test a TCP port, run Test-NetConnection -ComputerName <host> -Port <number> in Windows PowerShell and read the TcpTestSucceeded field: True means a TCP handshake completed from your machine to that port. Everything else in the output tells you how the connection was attempted, which matters once a test fails.

At PacketTools, we use the same checks in production networks to verify VPNs and analyze routing. You will learn the exact syntax for PowerShell Test-NetConnection, what each field proves about TCP connectivity, where the cmdlet stops (UDP, TLS, logins), and how to pair it with outside-in checks from our network tools.

How Do You Test a Host or TCP Port?

You test a host with -ComputerName alone (an ICMP ping) and a service by adding -Port or -CommonTCPPort. The cmdlet runs only on Windows, accepts names or IPs, and has a short alias, tnc.

Check Windows Availability and Basic Command Syntax

The Test-NetConnection cmdlet lives in the NetTCPIP module. It arrived with PowerShell 4 on Windows 8.1 and Server 2012 R2, and route diagnostics were added in Windows 10 and Server 2016. It is a Windows cmdlet: PowerShell 7 on Linux or macOS does not include it.

Microsoft documents four parameter sets: ICMP, CommonTCPPort, RemotePort, and NetRouteDiagnostics. You pick one per run:

Test-NetConnection [[-ComputerName] <String>] [-TraceRoute] [-Hops <Int32>] [-InformationLevel <String>]
Test-NetConnection [[-ComputerName] <String>] [-CommonTCPPort] <String> [-InformationLevel <String>]
Test-NetConnection [[-ComputerName] <String>] -Port <Int32> [-InformationLevel <String>]
Test-NetConnection [[-ComputerName] <String>] [-DiagnoseRouting] [-ConstrainSourceAddress <String>] [-ConstrainInterface <UInt32>] [-InformationLevel <String>]

Run it with no arguments and it pings internetbeacon.msedge.net, a quick way to confirm local network, DNS, and internet access in one line.

Run a Ping Test with -ComputerName

-ComputerName is positional, so tnc 10.0.0.5 and Test-NetConnection -ComputerName 10.0.0.5 are identical. It also accepts the aliases RemoteAddress and cn.

PS C:> Test-NetConnection -ComputerName fileserver01

ComputerName           : fileserver01
RemoteAddress          : 10.20.4.15      # what DNS resolved
InterfaceAlias         : Ethernet        # NIC used to send
SourceAddress          : 10.20.1.88      # your IP on that path
PingSucceeded          : True            # ICMP echo answered
PingReplyDetails (RTT) : 2 ms

A failed ping prints warnings and PingSucceeded : False. Treat that as “ICMP got no reply,” since many firewalls drop echo requests by design. For browser-based reachability from outside your network, a ping test gives a second vantage point.

Test HTTPS, SSH, RDP, and WinRM Ports

Port tests answer the question users care about: does the service accept connections? Common examples:

Service Command
HTTPS tnc web01 -Port 443
SSH tnc linux01 -Port 22
RDP tnc rdsh01 -CommonTCPPort RDP (3389)
WinRM (HTTP) tnc srv01 -CommonTCPPort WINRM (5985)
SMB tnc nas01 -CommonTCPPort SMB (445)
SQL Server tnc sql01 -Port 1433

Annotated output for an RDP check looks like this:

PS C:> Test-NetConnection rdsh01 -CommonTCPPort RDP

ComputerName     : rdsh01
RemoteAddress    : 10.20.6.40
RemotePort       : 3389
InterfaceAlias   : Ethernet
SourceAddress    : 10.20.1.88
TcpTestSucceeded : True     # SYN, SYN-ACK, ACK completed

If you change the listener, remember the RDP port shortcut still targets 3389; use -Port with your custom number.

Choose -Port or -CommonTCPPort

-CommonTCPPort accepts only four values: HTTP, RDP, SMB, and WINRM. There is no HTTPS or SSH keyword, so -Port 443 and -Port 22 are your options there.

In practice, -Port is the safer habit. It makes the exact port visible in scripts and tickets, and it works for any TCP service. Keep -CommonTCPPort for fast interactive checks of those four Windows services. For an external view of whether a port is reachable from the internet, an open port checker complements the client-side test.

What Do the Connection Results Actually Prove?

A network engineer checks a laptop beside networking equipment.

Each field proves one narrow thing from one machine at one moment. TcpTestSucceeded and PingSucceeded measure different protocols, SourceAddress tells you which identity the network saw, and none of them confirms TLS, authentication, or UDP.

Read Annotated Output with -InformationLevel Detailed

Adding -InformationLevel Detailed exposes the extra fields that explain why a test behaved the way it did:

PS C:> Test-NetConnection web01 -Port 443 -InformationLevel Detailed

ComputerName            : web01
RemoteAddress           : 10.20.5.10        # address actually tested
RemotePort              : 443
NameResolutionResults   : 10.20.5.10        # every address DNS returned
                          10.20.5.11
MatchingIPsecRules      :                   # IPsec rules applied, if any
NetworkIsolationContext : Private Network
IsAdmin                 : False
InterfaceAlias          : Ethernet
SourceAddress           : 10.20.1.88
NetRoute (NextHop)      : 10.20.1.1         # gateway chosen for this route
TcpTestSucceeded        : True

NameResolutionResults is the field people skip. When DNS returns several addresses, the cmdlet tests one, so a pass against 10.20.5.10 says nothing about 10.20.5.11. Test each IP directly when you suspect one backend node.

Interpret TcpTestSucceeded and PingSucceeded Independently

When you run a port test and ICMP is blocked, you see a ping warning and TcpTestSucceeded : True. That combination is normal: the cmdlet pings first, then attempts the TCP handshake.

PingSucceeded TcpTestSucceeded What it suggests
True True Host reachable, port accepts connections
False True ICMP filtered; service is reachable
True False Host up; nothing listening, or port filtered
False False Host down, route broken, or everything filtered

A False TCP result has two common causes. A firewall silently dropping the SYN produces a timeout. A host with no listener sends a reset, the same condition behind a connection refused error in applications. The cmdlet reports both as False, so you need host-side evidence to tell them apart.

Distinguish the Source Address from the Destination Address

RemoteAddress is the destination you tested. SourceAddress is the local IP your machine used to reach it, and that is the address the target’s firewall evaluated.

This matters on multi-homed servers and VPN clients. A laptop on Wi-Fi plus a VPN adapter shows a different SourceAddress and InterfaceAlias depending on routes. If the firewall allows 10.50.0.0/16 (VPN) but your test left from 192.168.1.82 (home Wi-Fi), the failure is correct behavior. A success also proves only that this source works; a server in another subnet may still be blocked.

Understand Why TCP Success Does Not Validate TLS, Logins, or UDP

TcpTestSucceeded : True means the three-way handshake completed. Nothing more is sent. The cmdlet does not negotiate TLS, check a certificate, send an HTTP request, or authenticate.

So port 443 can pass while browsers fail on an expired certificate or a proxy that intercepts traffic. Port 3389 can pass while RDP rejects your credentials. Load balancers and proxies also complete handshakes on behalf of dead backends.

Test-NetConnection has no UDP mode. It cannot verify DNS over UDP 53, NTP on 123, or SNMP on 161, and UDP gives no handshake to confirm anyway. For UDP services, query the protocol itself, for example with Resolve-DnsName -Server.

How Do You Trace Paths and Diagnose Route Selection?

A network engineer examines branching connections between routers and servers on a monitor.

Use -TraceRoute to list the hops between you and the target, and -DiagnoseRouting to see why Windows picked a particular route and source address. The first looks outward across the network; the second explains decisions made inside your own host.

Trace Network Hops with -TraceRoute and Compare tracert

Route tracing sends ICMP echo requests with increasing TTL values, collecting the router that answers at each step:

PS C:> Test-NetConnection 8.8.8.8 -TraceRoute -Hops 15

ComputerName           : 8.8.8.8
RemoteAddress          : 8.8.8.8
InterfaceAlias         : Ethernet
SourceAddress          : 10.20.1.88
PingSucceeded          : True
PingReplyDetails (RTT) : 11 ms
TraceRoute             : 10.20.1.1        # your gateway
                         203.0.113.1      # ISP edge
                         0.0.0.0          # hop did not reply
                         8.8.8.8

-Hops caps the TTL (default 30). Hops shown as 0.0.0.0 stopped answering; that is common for routers that rate-limit ICMP and is not a fault by itself.

The trade-off against tracert is latency data. tracert prints three round-trip times per hop, while -TraceRoute returns only addresses. Use tracert when you are chasing a slow hop; use the cmdlet when you want hop lists as objects in a script. Our guide on how to trace a route covers reading path output in depth.

Inspect Route and Source Address Selection with -DiagnoseRouting

-DiagnoseRouting skips sending traffic to the target and instead asks the Windows stack how it would route there:

PS C:> Test-NetConnection www.contoso.com -DiagnoseRouting -InformationLevel Detailed

SelectedSourceAddress        : 2001:4898:e0:79:f17c:d212:8743:43c2
OutgoingInterfaceIndex       : 4
SelectedNetRoute             : DestinationPrefix: ::/0 NextHop: fe80::200:5eff:fe00:202
RouteSelectionEvents         : Route [...InterfaceIndex: 4...] is preferred over
                               Route [...InterfaceIndex: 5...] Reason: RouteOrder.
SourceAddressSelectionEvents : Source address 2001:...:43c2 is preferred over
                               fe80::f17c:... Rule = 2.0.
RouteDiagnosticsSucceeded    : True

RouteSelectionEvents names the reason one route won, such as metric or route order. SourceAddressSelectionEvents cites the RFC 6724 rule used to pick the source IP. This is how you prove that a split-tunnel VPN, a stale static route, or a second NIC is steering traffic away from the path you expected.

Apply -ConstrainInterface and -ConstrainSourceAddress to Route Diagnostics

Constraints force the diagnosis through a specific interface or source IP, letting you ask “what if traffic left this way?”

Get-NetIPInterface | Select ifIndex, InterfaceAlias, AddressFamily
Test-NetConnection www.contoso.com -DiagnoseRouting -ConstrainInterface 5 -InformationLevel Detailed
Test-NetConnection www.contoso.com -DiagnoseRouting -ConstrainSourceAddress 10.50.2.14

With -ConstrainInterface 5, Microsoft’s example output shows the default route on interface 4 marked as blocked with Reason: InterfaceConstraint, and the interface 5 route selected. If RouteDiagnosticsSucceeded turns False under a constraint, that interface has no usable route to the destination.

How Can You Script Checks and Choose Related Commands?

For scripts, -InformationLevel Quiet returns a plain Boolean you can branch on, and a foreach loop handles host and port matrices. Pair it with Test-Connection in PowerShell 7 and Get-NetTCPConnection on the target for a full picture.

Return a Boolean with -InformationLevel Quiet

Quiet mode drops the object and returns True or False, which keeps if statements readable:

if (Test-NetConnection sql01 -Port 1433 -InformationLevel Quiet -WarningAction SilentlyContinue) {
    Invoke-Sqlcmd -ServerInstance sql01 -Query "SELECT @@VERSION"
} else {
    Write-Warning "sql01:1433 unreachable from $env:COMPUTERNAME"
}

Add -WarningAction SilentlyContinue, because ping warnings otherwise flood the console when ICMP is filtered. With -Port, the Boolean reflects the TCP result.

Check Multiple Hosts and Ports with Explicit Loops

-ComputerName takes one string and -Port takes one integer, so you loop explicitly. Keep the full object for evidence:

$targets = 'dc01','dc02','web01'
$ports   = 53, 88, 389, 443, 445

$results = foreach ($h in $targets) {
    foreach ($p in $ports) {
        $r = Test-NetConnection $h -Port $p -WarningAction SilentlyContinue
        [pscustomobject]@{
            Host      = $h
            Port      = $p
            Remote    = $r.RemoteAddress
            Source    = $r.SourceAddress.IPAddress
            TcpOpen   = $r.TcpTestSucceeded
        }
    }
}
$results | Export-Csv .port-check.csv -NoTypeInformation

Recording Source in the CSV saves arguments later about which segment ran the test. Closed ports wait for a timeout (about 20 seconds each), so large matrices run slowly; use background jobs or ForEach-Object -Parallel in PowerShell 7.

Compare Test-NetConnection with Test-Connection Across PowerShell Versions

PowerShell Test-Connection is the cross-platform cmdlet; it changed a lot between versions:

Capability Test-NetConnection Test-Connection (5.1) Test-Connection (7.x)
Platform Windows Windows Windows, Linux, macOS
ICMP ping Yes Yes Yes
TCP port test -Port No -TcpPort
Traceroute -TraceRoute No -Traceroute
Route diagnostics -DiagnoseRouting No No
Multiple targets per call No Yes Yes

In PowerShell 7, the Test-Connection cmdlet offers a TcpPort parameter set with -Quiet and -TimeoutSeconds, and -Traceroute arrived in PowerShell 6.0. Its -Quiet ping returns $true if any of the four pings succeed. On Linux jump hosts, Test-Connection -TcpPort is your option; on Windows, Test-NetConnection still gives richer route context.

Use Get-NetTCPConnection and PowerShell Remoting for Follow-Up Checks

When a client test fails, look at the server. Get-NetTCPConnection shows whether anything listens:

Invoke-Command -ComputerName web01 -ScriptBlock {
    Get-NetTCPConnection -LocalPort 443 -State Listen |
        Select LocalAddress, LocalPort, OwningProcess
}

No rows means no listener, so the fix is on the service. A listener on 127.0.0.1 only means it refuses remote clients. A listener on 0.0.0.0 plus a failed client test points at a firewall. PowerShell remoting also lets you run Test-NetConnection from another server, which is the fastest way to compare two source perspectives; this layered approach is the core of methodical network troubleshooting.

How Do You Isolate DNS, Firewall, and Service Failures?

Isolate failures by changing one variable per test: swap the hostname for its IP to rule DNS in or out, then compare client results with server listener state and an outside-in check. Each comparison removes one layer from the suspect list.

Compare Hostname and IP Tests to Investigate DNS Resolution

Run the same port test twice:

tnc app.corp.local -Port 443 -InformationLevel Detailed
tnc 10.20.5.10    -Port 443

If the IP passes and the name fails, DNS resolution is the problem. A failure reads “Name resolution of app.corp.local failed” with no RemoteAddress. If the name resolves to an unexpected address, compare NameResolutionResults against Resolve-DnsName app.corp.local -Server 10.20.0.10 or the nslookup command pointed at each DNS server.

Watch for the hosts file, split-horizon zones returning public IPs internally, and cached stale records (Clear-DnsClientCache). Steps to troubleshoot DNS issues apply once the IP test proves the service itself works.

Separate Failed TCP Tests from Firewall and Listener Evidence

A False result needs corroboration before you blame the firewall. Work through this order:

  1. Listener: Get-NetTCPConnection -LocalPort <port> -State Listen on the target.
  2. Host firewall: Get-NetFirewallRule -Enabled True -Direction Inbound filtered by port, or test from the server to itself.
  3. Path firewall: repeat the test from a host in the same subnet as the target. Pass there plus fail from your subnet points at an ACL or network firewall.
  4. Source identity: confirm SourceAddress falls in the range the rule allows.

Timing helps too. An immediate False suggests a reset from a host with no listener; a roughly 20-second wait suggests packets dropped in transit.

Compare Client-Side Results with PacketTools DNS, Ping, Traceroute, and TLS Checks

Test-NetConnection only shows your side. PacketTools tools run from outside your network, so you can compare perspectives: if a public web server passes internally but fails externally, the edge firewall or NAT rule is the suspect.

Useful pairings:

  • DNS Lookup and the DNS propagation checker confirm what public resolvers return after a record change.
  • Ping Test and Traceroute show reachability and path from the public internet.
  • SSL/TLS Checker covers the gap TcpTestSucceeded leaves: certificate validity and chain.

No IPs, queries, or test data are logged or stored, and no sign-in is required.

Verify the Service from the Source That Matters

Run your test from the machine, subnet, or VPN that has the problem, because TCP connectivity is always a statement about one source and one destination. A pass from your admin workstation proves nothing about a branch office client.

Before closing a ticket, capture three things:

  • TcpTestSucceeded for the exact host and port, tested by IP and by name.
  • SourceAddress and InterfaceAlias, so everyone knows which path was tested.
  • A protocol-level check for anything beyond the handshake: TLS, login, or a UDP query.

Use -InformationLevel Quiet for scripts, Detailed for evidence, -DiagnoseRouting when the wrong interface is suspected, and a server-side Get-NetTCPConnection when the port fails. Then compare against an external test so both perspectives are documented.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *