To open a port in Windows Firewall from PowerShell, run New-NetFirewallRule from a PowerShell window opened as Administrator:
New-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" -Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow
That is the minimum: a display name, a direction, the protocol and port, and an action. To see what it would do without changing anything, add -WhatIf, which works without admin rights too.
A Safer Version: One Subnet, Created Once
The script below creates an inbound rule for the default SQL Server port, 1433, allows only one subnet to use it, and checks the rule does not already exist first:
# Define the allowed IP range (Modify as needed)
$AllowedIPRange = "10.19.24.0/24"
# One variable for the name, so the check and the rule can never drift apart
$RuleName = "Allow Inbound SQL (1433)"
# Check if the rule already exists; if not, create it
if (-not (Get-NetFirewallRule -DisplayName $RuleName -ErrorAction SilentlyContinue)) {
New-NetFirewallRule `
-DisplayName $RuleName `
-Direction Inbound `
-Protocol TCP `
-LocalPort 1433 `
-Action Allow `
-RemoteAddress $AllowedIPRange
}Use one variable for the name. If the Get-NetFirewallRule check and the New-NetFirewallRule call use even slightly different display names, the check never matches and the rule is created every time the script runs. Windows allows duplicate display names (the unique key is the Name property), so you quietly collect copies rather than getting an error. Thanks to James in the comments for catching this one.

For better security, always restrict firewall rules to trusted IP ranges rather than allowing all inbound traffic. The screenshot shows a rule allowing every address on 1433. If you are only troubleshooting, you could temporarily disable the Windows Firewall for a quick connection test instead.
The Rules a DBA Actually Writes
# SQL Server default instance
New-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" `
-Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow
# SQL Browser, needed for named instances
New-NetFirewallRule -DisplayName "SQL Browser (UDP 1434)" `
-Direction Inbound -Protocol UDP -LocalPort 1434 -Action Allow
# an Availability Group endpoint
New-NetFirewallRule -DisplayName "AG Endpoint (TCP 5022)" `
-Direction Inbound -Protocol TCP -LocalPort 5022 -Action Allow
# lock it to one subnet rather than the whole network
New-NetFirewallRule -DisplayName "SQL Server (app subnet only)" `
-Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow `
-RemoteAddress 10.20.30.0/24Profiles: the Setting People Miss
A rule applies to network profiles, and the default is all of them. On a server that is usually right; on a laptop it means the rule is live on any coffee-shop network too. Narrow it with -Profile:
New-NetFirewallRule -DisplayName "SQL Server (domain only)" `
-Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow `
-Profile DomainCheck the Rule with Get-NetFirewallRule
To view your new rule, use Get-NetFirewallRule:
# what inbound rules are enabled right now
Get-NetFirewallRule -Direction Inbound -Enabled True |
Select-Object DisplayName, Direction, Action, Profile | Format-Table -AutoSize
# the port on the rule you just made
Get-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" |
Get-NetFirewallPortFilterThe first lines of the first command, run on Windows 11:
DisplayName Direction Action Profile ----------- --------- ------ ------- Wi-Fi Direct Spooler Use (In) Inbound Allow Public Core Networking - IPv6 (IPv6-In) Inbound Allow Any Delivery Optimization (UDP-In) Inbound Allow Any

To find every rule that mentions a port, start from the port filters. Run this one as Administrator: without elevation Get-NetFirewallPortFilter on its own returns Access is denied, even though Get-NetFirewallRule works.
Get-NetFirewallPortFilter | Where-Object LocalPort -eq 1433 |
Get-NetFirewallRule | Select-Object DisplayName, Enabled, Direction, ActionChange, Disable or Remove a Rule
# narrow an existing rule to one subnet Set-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" -RemoteAddress 10.20.30.0/24 # turn it off but keep it Disable-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" # turn it back on Enable-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" # remove it entirely, -WhatIf first Remove-NetFirewallRule -DisplayName "SQL Server (TCP 1433)" -WhatIf Remove-NetFirewallRule -DisplayName "SQL Server (TCP 1433)"
A name that matches nothing is an error, not a silent no-op: No MSFT_NetFirewallRule objects found with property 'DisplayName' equal to .... A display name that matches several rules changes all of them.
Opening the port is only half of it. If SQL Server is still not reachable after the rule is in place, check that TCP/IP is enabled in SQL Server Configuration Manager and that the instance is listening on the port you opened. A firewall rule for a port nothing listens on looks identical to a firewall problem. To test from the other machine, see Test If a Port Is Open with PowerShell (Test-NetConnection).
I write about the Windows Firewall often enough for it to have its own tag on here. In a corporate network you may also need the network team to open the flow between servers, since the Windows Firewall is only the last hop.
If a rule is not doing what you expect, paste the Get-NetFirewallRule output into Claude Code and ask what it allows. That’s what it’s there for.
Frequently Asked Questions
Do I need to run PowerShell as Administrator?
Yes for New-, Set-, Enable-, Disable- and Remove-NetFirewallRule. Get-NetFirewallRule works without it, and so does -WhatIf.
What happens if I create the same rule twice?
You get two rules. Display names do not have to be unique, so there is no error. Check first with Get-NetFirewallRule -DisplayName, as in the script above.
Does New-NetFirewallRule work in PowerShell 7?
Yes, on Windows. The NetSecurity module loads in both Windows PowerShell 5.1 and PowerShell 7.
What is the netsh equivalent?
netsh advfirewall firewall add rule name="SQL Server (TCP 1433)" dir=in action=allow protocol=TCP localport=1433. It still works, but the PowerShell cmdlets are easier to check and script.
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.
Leave a Reply