Efficiently manage and secure Windows systems with this PowerShell script, automating the creation and management of firewall rules for IT admins and security teams.
This PowerShell script is an essential tool for IT administrators, security teams, and MSPs to efficiently manage and configure Windows Firewall settings. Automate the creation of inbound and outbound firewall rules, enable or disable the firewall, and secure your systems against unauthorized access. This guide provides a detailed breakdown of the commands and best practices for network security.
# -----------------------------------------------------------------------------
# Script Name: Configure-WindowsFirewall.ps1
# Description: Automates the creation and management of Windows Firewall rules.
# Author: Zecurit Team
# Date: August 14, 2025
# -----------------------------------------------------------------------------
# Section 1: Enable the Windows Firewall
# This command ensures the firewall is active on all network profiles (Domain, Public, Private)
Set-NetFirewallProfile -Profile Domain, Public, Private -Enabled True
# Section 2: Create a new inbound rule for Remote Desktop (RDP)
# This rule allows inbound RDP connections on TCP port 3389, a common task for remote administration.
New-NetFirewallRule -DisplayName "Allow RDP" -Direction Inbound -Protocol TCP -LocalPort 3389 -Action Allow
| Command | Description |
|---|---|
| Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True | Ensures the firewall is enabled for all network profiles. |
| New-NetFirewallRule -DisplayName "Allow RDP" -Direction Inbound -Protocol TCP -LocalPort 3389 -Action Allow | Allows RDP connections. |
| New-NetFirewallRule -DisplayName "Block Malicious IP" -Direction Outbound -RemoteAddress 192.168.1.100 -Action Block | Blocks outbound connections to a specific IP address. |
| New-NetFirewallRule -DisplayName "Allow Web Traffic" -Direction Inbound -Protocol TCP -LocalPort 80,443 -Action Allow | Allows web traffic on HTTP and HTTPS ports. |
| Remove-NetFirewallRule -DisplayName "Allow RDP" | Demonstrates how to remove a rule. |
| Get-NetFirewallRule | Select-Object DisplayName, Enabled, Direction, Action, Profile |
Here are some potential use cases:
By using this script, administrators can effectively manage firewall settings and boost security across Windows devices.
Upload this script to Zecurit's Script Repository and execute it across hundreds of endpoints in minutes. Support for PowerShell, Bash, Python,and more with full audit trails and scheduling.
You can enable the firewall for all network profiles using: "Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True"
Use this command to allow Remote Desktop (RDP) connections: "New-NetFirewallRule -DisplayName "Allow RDP" -Direction Inbound -Protocol TCP -LocalPort 3389 -Action Allow"
Yes, you can block outbound traffic to an IP using: "New-NetFirewallRule -DisplayName "Block Malicious IP" -Direction Outbound -RemoteAddress 192.168.1.100 -Action Block"
Run the following command to display all firewall rules: "Get-NetFirewallRule | Select-Object DisplayName, Enabled, Direction, Action, Profile"
Yes, use the following command to delete a specific rule: "Remove-NetFirewallRule -DisplayName "Allow RDP""