Created
June 2, 2026 15:59
-
-
Save ZipFile/a5c218519c49e99162fc5e14b6aa16eb to your computer and use it in GitHub Desktop.
Windows Firewall Cleanup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Remove rules for file paths that are no longer exist | |
| # | |
| # Usage: windows-firewall-cleanup.ps1 [-y] | |
| # use -y parameter to actually remove the rules | |
| # | |
| # Roughly based on: | |
| # * https://gist.github.com/xbb/246529a0c91305d53d428e116249c4ba | |
| # * https://web.archive.org/web/20260218161026/https://wangxiaohu.com/blog/?p=420 | |
| # | |
| # Don't forget to run as admin and `Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass` | |
| param( | |
| [switch]$y = $false | |
| ) | |
| $removeArgs = if ($y) { | |
| @{ Verbose = $true } | |
| } else { | |
| @{ WhatIf = $true } | |
| } | |
| $unusedFilters = | |
| Get-NetFirewallApplicationFilter | | |
| Where-Object { | |
| $program = [Environment]::ExpandEnvironmentVariables($_.Program) | |
| $_.Program -notin @("Any", "System") -and | |
| [System.IO.Path]::IsPathRooted($program) -and | |
| -not (Test-Path -LiteralPath $program -PathType Leaf) | |
| } | |
| $unusedFilters | | |
| ForEach-Object { [Environment]::ExpandEnvironmentVariables($_.Program) } | | |
| Sort-Object -Unique | |
| $unusedFilters | Remove-NetFirewallRule @removeArgs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment