How to Create a New Firewall Rule with PowerShell

In this bog post I’m sharing a guide on how to create a new Firewall Rule with PowerShell. We’ll create a new inbound rule using a PowerShell script for the local Windows Firewall, to allow SQL Server (port 1433).

Script to Create a New Firewall Rule for SQL Server

The PowerShell script below creates a new inbound rule on the local server which allows port 1433 (default Microsoft SQL Server port). It ensures that only devices from a specific subnet (e.g., 10.19.24.0/24) can access SQL Server.

Before running the script, make sure to open PowerShell as Administrator, otherwise the script won’t execute.

# Define the allowed IP range (Modify as needed)
$AllowedIPRange = "10.19.24.0/24"

# Check if the rule already exists; if not, create it
if (-not (Get-NetFirewallRule -DisplayName "Allow Inbound SQL (1433)" -ErrorAction SilentlyContinue)) { 
    New-NetFirewallRule `
        -DisplayName "Allow Inbound SQL - 1433" `
        -Direction Inbound `
        -Protocol TCP `
        -LocalPort 1433 `
        -Action Allow `
        -RemoteAddress $AllowedIPRange
}

This script is checking if the Firewall Rule already exists before creating it.

Create New Firewall Rule PowerShell

For better security, always restrict firewall rules to trusted IP ranges instead of allowing all inbound traffic. The screenshot shows allowing all IPs on 1433, if troubleshooting an issue you could temporarily disable the Windows Firewall and quickly perform your connection test.

To view your newly created Firewall rule, we can use the Get-NetFirewallRule cmdlet:

PowerShell Get-NetFirewallRule Example

Relevant MS Docs:
New-NetFirewallRule
Set-NetFirewallRule
Remove-NetFirewallRule

I seem to write about the Windows Firewall often enough for it to have a blog tag on here. Feel free to check out my other Firewall related posts for more info as linked!

One other thing to add to this is, if you’re working within a Corporate environment, you might need to ask network teams to open up network flow between servers. For more information on this, checkout my other blog post: How to Test Connectivity to Remote Server Ports with PowerShell


Comments

Leave a Reply

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

Recent Posts
Categories
Tags

Always On Availability Groups (AAG) (4) AWS (4) AWS Redshift (6) Database Admin (72) Database Backups & Recovery (14) Database Mirroring (2) Error Messages (5) Failover Cluster Instances (FCI) (1) Git Commands (6) Importing & Exporting Data (2) Linked Servers (3) Linux Administration (2) Logging & Monitoring (1) Microsoft Patching (2) MySQL (4) Postgres (6) PowerShell Scripts (2) SQL Certificates & Encryption (3) SQL Server Agent (5) SQL Server CDC (2) SQL Server Data Types (2) SQL Server Management Studio (SSMS) (17) SQL Server Networking (3) SQL Server on Linux (1) SQL Server Patching (2) SQL Server Performance Tuning (6) SQL Server Processes (SPIDs) (7) SQL Server Replication (2) SQL Server Scripts (13) SQL Server Security (4) SQL Server Storage (10) Windows Admin (20) Windows Authentication (2) Windows Automation (1) Windows Events (2) Windows Firewall (4) Windows Subsystem for Linux (WSL) (18)