Skip to content

Instantly share code, notes, and snippets.

@ZipFile
Created June 2, 2026 15:59
Show Gist options
  • Select an option

  • Save ZipFile/a5c218519c49e99162fc5e14b6aa16eb to your computer and use it in GitHub Desktop.

Select an option

Save ZipFile/a5c218519c49e99162fc5e14b6aa16eb to your computer and use it in GitHub Desktop.
Windows Firewall Cleanup
# 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