PowerShell Firewall administration
The NetSecurity module manages Windows Firewall. Get-NetFirewallRule lists rules, New-NetFirewallRule creates them, and Set-NetFirewallRule changes them.
Documentation: Microsoft Learn
Overview
The design catches people out. A rule object does not carry its ports or addresses; those live in separate filter objects. To see which port a rule covers you pipe the rule into Get-NetFirewallPortFilter, and for addresses into Get-NetFirewallAddressFilter. So finding the rule that blocks port 445 is a two-step query, not a one-line filter on the rule list.
Profiles are the second trap. Rules apply per profile: Domain, Private, Public. A rule can be Enabled and still have no effect because the active profile is not one it applies to. Get-NetFirewallProfile shows which profile is active, and checking it first saves you from debugging a rule that was never in scope.
When creating rules, be specific. New-NetFirewallRule with -RemoteAddress limits who can connect, and a rule scoped to a subnet is a very different thing from one open to any address.
Rules created by PowerShell are permanent. Remove them with Remove-NetFirewallRule when you are done testing, or they quietly outlive their reason.
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
[pscustomobject]@{
Rules = 'Get-NetFirewallRule -Enabled True -Direction Inbound'
Ports = 'Get-NetFirewallRule | Get-NetFirewallPortFilter'
Profiles = 'Get-NetFirewallProfile'
Note = 'a rule can be enabled and inactive if its profile is not current'
} | Format-List- Stage 1[pscustomobject]@{
Positional values or no named parameters - Stage 2Format-List
Positional values or no named parameters
An example to inspect
Preview the example. Sign in for the full lesson and activities.
[pscustomobject]@{
Rules = 'Get-NetFirewallRule -Enabled True -Direction Inbound'
Ports = 'Get-NetFirewallRule | Get-NetFirewallPortFilter'
Profiles = 'Get-NetFirewallProfile'
Note = 'a rule can be enabled and inactive if its profile is not current'
} | Format-ListWhat happens
This prints the three queries you need together, because the answer is spread across three cmdlets. The Ports line shows the piping pattern that joins a rule to its port filter. The note is the failure mode: enabled is not the same as active.
Worth remembering
- Ports and addresses live in filter objects, not on the rule.
- Pipe a rule into Get-NetFirewallPortFilter to see its ports.
- Rules apply per profile; check the active profile first.
- Scope new rules with -RemoteAddress rather than leaving them open.
Primary documentation
Try it
Expected output · prints command text; does not execute these commands
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Prints the three firewall queries and the enabled-versus-active caveat.
Show reference output
Expected output · prints command text; does not execute these commands
Rules : Get-NetFirewallRule -Enabled True -Direction Inbound Ports : Get-NetFirewallRule | Get-NetFirewallPortFilter Profiles : Get-NetFirewallProfile Note : a rule can be enabled and inactive if its profile is not current
Keep going
Next: Windows Update PowerShell commands: inventory and checksCheck your understanding
An inbound allow rule exists, but a connection still fails. What else matters?
Check the active profile, rule scope, protocol and port, competing block rules, network path and whether the service is listening.