Test If a Port Is Open with PowerShell (Test-NetConnection)

📜Part of the PowerShell Complete Guide, every PowerShell post on this site, grouped by the job you are doing.

To check if a port is open from PowerShell, run Test-NetConnection with the server and the port:

PowerShell
# test a port by name
Test-NetConnection -ComputerName localhost -Port 1433

# the same test with the short alias and an IP address
tnc 127.0.0.1 -Port 9999

Look at TcpTestSucceeded. True means something answered on that port. False means the connection failed: nothing is listening, or a firewall is in the way.

PowerShell Test-NetConnection on port 1433 succeeding and tnc on port 9999 failing
Run against this machine: port 1433, where SQL Server is listening, returns TcpTestSucceeded True, and port 9999, where nothing is listening, returns False.

A failing test prints a WARNING: TCP connect to ... failed line and also tries a ping, so the output is longer. Against a remote server it can sit for a while before it gives up, so give it time before deciding it has hung.

Ports a DBA Checks Most

  • SQL Server: 1433, and 5022 for Availability Group endpoints
  • PostgreSQL: 5432
  • MySQL: 3306
  • Oracle: 1521
  • Redshift: 5439

Test-NetConnection only tests TCP. The SQL Server Browser listens on UDP 1434, so a named instance lookup cannot be checked this way.

Get a Plain True or False

For a script, -InformationLevel Quiet returns just the result:

PowerShell
Test-NetConnection sqlserver01 -Port 1433 -InformationLevel Quiet

Test several ports in one go:

PowerShell
$server = 'localhost'
1433, 9999 | ForEach-Object {
    [pscustomobject]@{
        Port = $_
        Open = Test-NetConnection $server -Port $_ -InformationLevel Quiet -WarningAction SilentlyContinue
    }
}
Code
Port  Open
----  ----
1433  True
9999 False

For the common Windows ports there is a shortcut: -CommonTCPPort takes RDP, SMB, HTTP or WINRM instead of a number.

On Older Windows: No Test-NetConnection

Test-NetConnection arrived with Windows 8.1 and Windows Server 2012 R2. On anything older, the .NET TcpClient does the same job:

PowerShell
$client = New-Object System.Net.Sockets.TcpClient
try {
    $client.Connect('sqlserver01', 1433)
    'Port 1433 is open'
} catch {
    "Port 1433 is closed: $($_.Exception.InnerException.Message)"
} finally {
    $client.Close()
}

Below is the earlier version of this check failing on a Windows Server 2008 R2 test server. It had no SQL Server installed, so there was nothing listening on that port.

TCP Port Test PowerShell Windows Server 2008 R2 Failed

When the Test Fails

  • Nothing listening: check on the server itself that the service is running and on the port you expect. How to Check Listening Ports on Windows shows what is listening.
  • The local firewall: the server’s Windows Firewall needs an inbound rule. See Create a Firewall Rule with PowerShell (New-NetFirewallRule).
  • The network in between: firewalls, NAT gateways, cloud security groups and load balancers. In a corporate network this usually means an ACL request to the network team.
  • The wrong address: check RemoteAddress in the output. If DNS resolves the name to an address you did not expect, you tested the wrong machine.

If the test returns True and the application still cannot connect, the network is fine and the problem is in the application: the login, the instance name or the connection string.

If the output does not make sense, paste it into Claude Code and ask what it means. That’s what it’s there for.

Frequently Asked Questions

Does Test-NetConnection work in PowerShell 7?

Yes, on Windows. It comes from the NetTCPIP module, which loads in both Windows PowerShell 5.1 and PowerShell 7.

Can Test-NetConnection test a UDP port?

No, it only tests TCP. A UDP service such as the SQL Server Browser on 1434 does not reply to a connection test, so there is no simple open or closed answer for it.

How do I make the test give up sooner?

Test-NetConnection has no timeout parameter. With TcpClient you can set one: $c = New-Object System.Net.Sockets.TcpClient; $c.ConnectAsync('sqlserver01', 1433).Wait(1000); $c.Close() returns True if it connected within one second.

What about telnet?

The Telnet client is not installed on Windows by default, and Test-NetConnection does the same check with output you can script against.

Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

One response to “Test If a Port Is Open with PowerShell (Test-NetConnection)”

  1. […] Checkout my other post for more information on testing connectivity to remote ports using PowerShell. […]

Leave a Reply

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