How to Scope Windows Defender Firewall Rules to Programs and Services in Windows 11 Using PowerShell

In secure enterprise endpoint management and developer workstation hardening, defining firewall rules based solely on port numbers and IP subnets leaves systems open to process spoofing and unauthorized socket binding. For example, opening TCP port 8080 allows any user-space application or untrusted script to bind to that port and accept inbound connections unless the firewall rule is strictly bound to a designated executable binary. Conversely, system background services (such as Windows Time or DNS Client) should be restricted to the official service controller identity rather than generic host processes.

In Windows 11, the native PowerShell NetSecurity module separates application-level constraints from transport-layer definitions through the Get-NetFirewallApplicationFilter and Set-NetFirewallApplicationFilter cmdlets. Through this application filter object, administrators can restrict inbound and outbound traffic to specific executable file paths, bind rules to internal Windows background service names, or scope rules to modern Packaged App (MSIX/UWP) family names. This guide covers how to inspect application filter properties, bind rules to binaries and services, scope modern packaged apps, and audit application-scoped firewall rules.

Understanding Application Filters in the Windows Filtering Platform

In the Windows Filtering Platform (WFP), each firewall rule links to an underlying application filter (NetFirewallApplicationFilter). This filter evaluates packet flows against the initiating or receiving process identity:

  • Program: The absolute file path to the executable binary authorized to transmit or receive traffic (e.g., C:\Program Files\App\service.exe). The firewall engine verifies the exact binary path executing the socket call, rejecting packets if another process attempts to communicate on the same port.
  • Service: Binds the rule to a registered Windows service short name (e.g., W32Time or Spooler). This is essential for shared host processes like svchost.exe, ensuring only the specific targeted service thread within the shared process container is granted network access.
  • Package: Binds the rule to a modern Windows App (UWP or MSIX package family name, e.g., Microsoft.WindowsTerminal_8wekyb3d8bbwe). This enforces AppContainer sandbox boundaries, ensuring sandboxed applications cannot be spoofed by external binaries.

Inspecting Application Filters on Existing Firewall Rules

To inspect the application binding of any firewall rule, open an elevated PowerShell prompt (Run as Administrator) and pipe Get-NetFirewallRule to Get-NetFirewallApplicationFilter:

# Query application filter for the Windows Time service rule
Get-NetFirewallRule -DisplayName "Core Networking - Windows Time (UDP-In)" | 
    Get-NetFirewallApplicationFilter

The resulting object displays active program and service constraints:

Program       : %SystemRoot%\System32\svchost.exe
Package       : Any
Service       : W32Time
PolicyStore   : PersistentStore

Notice that while the executable path points to the generic svchost.exe binary, the rule strictly authorizes the W32Time service, preventing other services sharing svchost.exe from listening on port 123.

Scoping Existing Rules to Executable Binaries

To restrict an existing firewall rule so that it permits traffic exclusively when initiated by a specific application binary, use Set-NetFirewallApplicationFilter:

# Bind custom database rule to specific engine binary
Get-NetFirewallRule -DisplayName "Custom-Database-Service" | 
    Get-NetFirewallApplicationFilter | 
    Set-NetFirewallApplicationFilter -Program "C:\Program Files\PostgreSQL\16\bin\postgres.exe"

You can also use environment variable paths (such as %ProgramFiles% or %SystemRoot%) to ensure portability across different system drive letters:

# Use environment variable path for application binary
Get-NetFirewallRule -DisplayName "Internal-Nginx-Proxy" | 
    Get-NetFirewallApplicationFilter | 
    Set-NetFirewallApplicationFilter -Program "%ProgramFiles%\Nginx\nginx.exe"

Binding Firewall Rules to Windows Background Services

When securing background services that run under shared process wrappers (such as svchost.exe), hardcoding the program path alone is insecure because all hosted services share that executable. Binding the service name isolates network access to that specific service.

First, identify the exact short name of the Windows service using Get-Service:

# Retrieve service short name
Get-Service -DisplayName "Print Spooler" | Select-Object Name, Status

Then, bind the service name to the firewall rule’s application filter:

# Scope firewall rule to the Print Spooler service
Get-NetFirewallRule -DisplayName "File and Printer Sharing (Spooler-In)" | 
    Get-NetFirewallApplicationFilter | 
    Set-NetFirewallApplicationFilter -Program "%SystemRoot%\System32\spoolsv.exe" -Service "Spooler"

Scoping Rules to Modern MSIX and UWP Packages

Modern Windows applications running inside AppContainer sandboxes are identified by their Package Family Name. To permit network access exclusively for a packaged application:

  1. Retrieve the Package Family Name using Get-AppxPackage:
    # Query Package Family Name for Windows Terminal
    (Get-AppxPackage -Name "*WindowsTerminal*").PackageFamilyName
    
  2. Assign the package string to the application filter:
    # Scope rule to the modern Windows Terminal package
    Get-NetFirewallRule -DisplayName "Dev-Terminal-Listener" | 
        Get-NetFirewallApplicationFilter | 
        Set-NetFirewallApplicationFilter -Package "Microsoft.WindowsTerminal_8wekyb3d8bbwe"
    

Creating New Rules with Inline Application Filters

You can define program, service, and package bindings directly when creating a new rule using the -Program and -Service parameters of New-NetFirewallRule:

# Create inbound rule permitting custom API listener strictly for authorized binary
New-NetFirewallRule -DisplayName "Authorized-Node-API" `
    -Direction Inbound `
    -Action Allow `
    -Protocol TCP `
    -LocalPort 3000 `
    -Program "C:\Program Files\nodejs\node.exe" `
    -Profile Private, Domain

If another application attempts to open or listen on port 3000, Windows Defender Firewall will block incoming traffic because the process binary does not match node.exe.

Auditing Application-Scoped Firewall Rules

To audit all inbound firewall rules that are bound to specific executable binaries or services across your system, execute the following script:

# Audit all inbound rules with explicit program or service bindings
Get-NetFirewallRule -Direction Inbound -Action Allow -Enabled True | ForEach-Object {
    $Rule = $_
    $AppFilter = $Rule | Get-NetFirewallApplicationFilter
    if ($AppFilter.Program -ne "Any" -or $AppFilter.Service -ne "Any") {
        [PSCustomObject]@{
            DisplayName = $Rule.DisplayName
            Program     = $AppFilter.Program
            Service     = $AppFilter.Service
            Profile     = $Rule.Profile
        }
    }
} | Format-Table -AutoSize

Troubleshooting Common Application Filter Pitfalls

When working with application filters in Windows Defender Firewall, keep these key operational factors in mind:

  • Symlinks and Relative Paths: The Windows Filtering Platform checks the resolved canonical path of the binary. Using relative paths (e.g., .\service.exe) or unexpanded junction links can cause filter matching failures. Always provide absolute, canonical filesystem paths.
  • Process Path Changes on Updates: Applications that install updates into version-numbered directories (such as Discord, Slack, or specific browser updates installed in app-1.2.3\) will break application-scoped firewall rules when updated to a new directory. In such cases, use service bindings or parent directory wildcards if supported.
  • Windows Service Permissions: The service short name passed to -Service is case-sensitive and must match the registered Windows Service Control Manager name exactly, not the human-readable display name.

Using Set-NetFirewallApplicationFilter enables system administrators to enforce strict process-level boundaries, ensuring network ports remain accessible strictly to verified, authorized applications across Windows 11 environments.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.