← script library

Automate Windows Firewall Configuration with PowerShell

Security & AuditingDecember 24, 2024

In this post, we will showcase a PowerShell script that automates the configuration of the Windows Firewall on your system. Proper firewall configuration is essential for maintaining security and preventing unauthorized access to your network. This script allows administrators to swiftly enable or disable firewall rules, customize settings, and ensure that the appropriate ports are open for necessary applications, enhancing both security and functionality. Here is the PowerShell script for managing Windows Firewall rules:

powershell
# Define variables for the firewall configuration
$ruleName = "Allow Web Traffic"
$portNumber = 80
# Enable Windows Firewall
Set-NetFirewallProfile -Enabled True
# Create a new inbound firewall rule to allow web traffic
New-NetFirewallRule -DisplayName $ruleName -Direction Inbound -Protocol TCP -LocalPort $portNumber -Action Allow 
# Check if the rule was created successfully
Get-NetFirewallRule -DisplayName $ruleName | Format-List
Write-Host "Firewall rule '$ruleName' for port $portNumber created successfully."

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.