Manage Windows Firewall with PowerShell

< All Topics

Refer to NetSecurity Module for functionality provided on how to Manage Windows Firewall with PowerShell

Firewall Profile

Windows Firewall Profile is enabled by default on Domain, Private and Public with

  • Outbound Connection = Allow (Default)
  • Inbound Connection = Block (Default) with some of the core networking features allowed, like DHCP
Get-NetFirewallProfile | Select Name, Enabled

Third-party applications will automatically add Additional Rules, like AnyDesk during installation to allow it to function even with Windows Firewall enabled

Get-NetFirewallRule | ? DisplayName -like "AnyDesk" | Select DisplayName, Profile, Enabled

DisplayName Profile Enabled
----------- ------- -------
AnyDesk     Private    True
AnyDesk     Private    True
AnyDesk      Domain    True
AnyDesk      Domain    True

Windows Firewall Profile is applied based on the Network Connection Profile in Network and Sharing Center

Manage Windows Firewall with PowerShell

# List the Active Network Connection Profile
# List the Active Windows Firewall Profile
netsh advfirewall show currentprofile

Enable / Disable Firewall Profile

#Enable Firewall for all profile 
Set-NetFirewallProfile -Profile Domain,Private, Public -Enabled true
#Disable Firewall for all profile 
Set-NetFirewallProfile -Profile Domain,Private, Public -Enabled false

Predefined Firewall Rules / Groups

Several predefined Firewall Rule is provided by default

![Manage Windows Firewall with PowerShell)(

Check the Firewall Rules included in the default Remote Desktop Group

Get-NetFirewallRule -DisplayGroup "Remote Desktop" | Select DisplayName

Remote Desktop - User Mode (TCP-In)
Remote Desktop - User Mode (UDP-In)
Remote Desktop - Shadow (TCP-In)

List the detail configuration of Firewall Rule

# Detail configuration 
Get-NetFirewallRule -DisplayName "Remote Desktop - User Mode (TCP-In)"
# List TCP / UDP Ports defined
Get-NetFirewallRule -DisplayName "Remote Desktop - User Mode (TCP-In)" | Get-NetFirewallPortFilter

Enabled the pre-defined Group to allow inbound connection

# TCP & UDP 3389
Set-NetFirewallRule -DisplayGroup "Remote Desktop" -Enabled True
# TCP 445, 139, UDP 137, 138, 5355 and RPC EndPoint
Set-NetFirewallRule -DisplayGroup "File And Printer Sharing" -Enabled True
# TCP 135
Set-NetFirewallRule -Displaygroup "Windows Management Instrumentation (WMI)" -Enabled True

New Firewall Rule for Inbound

Create a new Firewall Rule

  • Display Name = Name to identified the functionality of Rule
  • Direction = Inbound / Outbound
  • Protocol = TCP, UCP, ICMPv4
  • Local Port = Port allowed for inbound access
  • Remote Address = Allow only certain IP Range to access
  • Action = Allow / Block
# Allow inbound TCP 8002 from &
New-NetFirewallRule -DisplayName "In-TCP-8002" -Direction Inbound -Protocol TCP -LocalPort 8002 -RemoteAddress, -Action Allow

# Allow inbound Ping
New-NetFirewallRule -DisplayName "In-ICMPv4-PING" -Direction Inbound -Protocol ICMPv4 -IcmpType 8 -Action Allow

Group Customized Firewall Rules

Create a new group called AVENTISLAB to include all the customize Firewall Rules for ease of management

$RuleName = "Allow inbound ICMPv4"
$RuleGroup = "AVENTISLAB"
Get-NetFirewallRule -DisplayName $RuleName | ForEach { $_.Group = $RuleGroup; Set-NetFirewallRule -InputObject $_ }

Customized Firewall Rules included in AVENTISLAB Group

Get-NetFirewallRule -DisplayGroup "AVENTISLAB" | Select DisplayName


Firewall Logs

Enable logging to record all Allow & Drop traffics in C:\Windows\system32\LogFiles\Firewall\pfirewall.log for Private Profile

Set-NetFirewallProfile -Name Private -LogFileName %systemroot%\system32\LogFiles\Firewall\pfirewall.
log -LogAllowed True -LogBlocked True

View last ten logs in Realtime for ALLOW traffics

Get-Content "C:\Windows\system32\LogFiles\Firewall\pfirewall.log" -last 10 -wait | Select-String "ALLOW"

#Fields: date time action protocol src-ip dst-ip src-port dst-port size tcpflags tcpsyn tcpack tcpwin icmptype icmpcode
info path

2020-04-18 14:03:25 ALLOW UDP 53083 53 0 - - - - - - - SEND
2020-04-18 14:03:25 ALLOW UDP 53083 53 0 - - - - - - - SEND
2020-04-18 14:03:25 ALLOW UDP fe80::4042:88a7:cfd8:ef53 ff02::1:3 59227 5355 0 - - - - - - - SEND

Previous Install SqlServer Module without Internet Access
Next Manage Windows Update with PowerShell
Table of Contents