Automate Windows Firewall Configuration with PowerShell

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:

# 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."