How to enable Windows Firewall using PowerShell

The Windows firewall is an essential element in the security of your computer system. Here are a few points that underline its importance:

  1. Protection against threats : Windows Firewall reduces the risk of network security threats by reducing a device’s attack surface. It provides threat protection for personal computers or any other computer system.
  2. Traffic filtering: The firewall acts as a security barrier, controlling and filtering the information traffic that flows between the Internet and your computer network. It applies specific filtering criteria that can be linked to IP addresses and data volumes, blocking unwanted traffic and potential cyber-attacks.
  3. Exception handling: The Windows firewall is often capable of handling necessary exceptions on its own, such as access to shares, where it will open the necessary flows on its own without needing our intervention.
  4. Blocking unknown or malicious applications: protects your PC against network attacks and intrusions by blocking connections from unknown or malicious applications.

In short, the Windows firewall is an essential security tool that helps protect your system from potential threats. So it’s important to keep it activated and properly configured to ensure maximum security.

This script is used to enable Windows Firewall through PowerShell, we can use this script for bulk run with RMM solution, for GPO Policy or even for Microsoft Intune Policy.

This script must be executed as System or Administrator group.

here is the source code :

# This script must be executed as System or Administrator group.

try
{
# Enable firewall for domain profile
Set-NetFirewallProfile -Profile Domain -Enabled True
Write-Host "Windows Domain Firewall activated successfully" -ForegroundColor Green
}
catch{Write-Host "Error while enabling Windows Domain Firewall" -ForegroundColor Red}

try
{
# Enable firewall for private profile
Set-NetFirewallProfile -Profile Private -Enabled True
Write-Host "Windows Private Firewall activated successfully" -ForegroundColor Green
}
catch{Write-Host "Error while enabling Windows Private Firewall" -ForegroundColor Red}

try
{
# Enable firewall for public profile
Set-NetFirewallProfile -Profile Public -Enabled True
Write-Host "Windows Public Firewall activated successfully" -ForegroundColor Green
}
catch{Write-Host "Error while enabling Windows Public Firewall" -ForegroundColor Red}

Thanks

Aymen EL JAZIRI (Microsoft MVP)
Aymen EL JAZIRI (Microsoft MVP)

Hi, I’m Aymen El Jaziri , a passionate System Administrator and Microsoft MVP, with years of hands-on experience in managing and securing modern IT infrastructures.
This blog is where I share technical guides, automation scripts, product reviews, and real-world solutions that help IT professionals simplify their day-to-day work and stay ahead in a fast-evolving cloud ecosystem.
Whether you’re here to troubleshoot an issue, improve your automation game, or learn new best practices , welcome in my blog !
Let’s build a stronger, smarter IT community together.
Feel free to connect with me on LinkedIn for more content, discussions, or collaboration opportunities.

Thanks

Aymen

Articles: 154