Open a firewall port on Windows (New-NetFirewallRule)
Create an inbound allow rule for a TCP/UDP port with one PowerShell cmdlet, and delete it cleanly afterwards.
01Command
$ New-NetFirewallRule -DisplayName "Allow HTTP 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow02What it does
Unlike reading rules, creating one is the other half of the NetSecurity module and is fully idempotent-safe: you can scope the rule with -RemoteAddress, -Profile (Domain/Private/Public) and -Program so it is far narrower than a GUI rule usually ends up. The undo is Remove-NetFirewallRule by the same DisplayName, or Disable-NetFirewallRule to keep it but turn it off. Rules created this way appear in Windows Defender Firewall with Advanced Security like any other.
03Risk
04Undo
$ Remove-NetFirewallRule -DisplayName "Allow HTTP 8080"05Sources
- New-NetFirewallRule | Microsoft LearnEvidence: Microsoft documents New-NetFirewallRule with parameters -Direction, -Protocol, -LocalPort and -Action to create an inbound allow rule.
Last verified: 2026-09-17 · Category: Network
06Related solutions
The PowerShell-native replacement for netstat: filter connections by state, port, or process and pipe results instead of parsing text.
Search inside text files and logs with regex, returning file, line number and match.
Create or update a .zip from any files or folder contents straight from PowerShell.