Disable all the unwanted Windows Firewall rules using PowerShell

Viewed 2224

In my Windows Firewall, I've created certain rules that give me more control over my PC. But my rules have become somewhat useless since Windows and other apps are kept adding rules that I don't want.
I've tried to prevent this from happening, but the only way I've found is to use a third-party tool like Tinywall, which isn't exactly what I'm looking for.

So, to fix this, I want to create a PowerShell script that will disable and rename all rules that are not added by me. This way, I can manage them easily.

Rules that are added by me can be easily recognized because all of them start with certain words.
In this case, let's assume it starts with either 'Sample XYZ' or 'Sample ABC'.

  • Sample XYZ - Windows Update
  • Sample ABC - MPC-HC
  • Sample ABC - Firefox
  • Sample XYZ - Windows News

So far, this is what I have done.

In this part, the script will filter all the rules that I have created and then it'll disable & block all other rules.
To my surprise, this is working as expected.

# This will get all firewall rules
$NR = Get-NetFirewallRule  

# This will exclude all the rules added by the user
$NR = $NR | Where-Object DisplayName -NotMatch "Sample ABC"  
$NR = $NR | Where-Object DisplayName -NotMatch "Sample XYZ"  

# Disable all other rules that are not added by the user
$NR | Set-NetFirewallRule -Enabled False

# Set rules' action to block
$NR | Set-NetFirewallRule -Action Block

These are the parts that don't work.

Task: Add a custom word to the beginning of the rules' display name
Example: If a rule name is 'Microsoft Photos', then it'll be renamed to 'IDWTFR - Microsoft Photos'.

# Add a custom word to the beginning of the rules' display name
# Custom word = 'IDWTFR - '
# Attempt 01: Fail
$NR | Set-NetFirewallRule -DisplayName "IDWTFR - " + $NR.DisplayName

# Attempt 02: Fail
$NR = $NR | ForEach-Object -MemberName DisplayName "IDWTFR - " + $NR.DisplayName | Set-NetFirewallRule

Task: Add unwanted rules to a group named 'Junk Rules'.

# Add to a group
# Attempt 01: Fail
$NR | Set-NetFirewallRule -DisplayGroup "Junk Rules"


To clarify it a bit more, this is the summary of what I am trying to do.

+-----------------------------+---------------------------+----------------+----------------+----------------+-------------+
|          Rule Name          |       New Rule Name       |     Group      |     Action     |     Status     | Created by  |
+-----------------------------+---------------------------+----------------+----------------+----------------+-------------+
| Sample XYZ - Windows Update | Same as before            | Same as before | Same as before | Same as before | User        |
| Sample ABC - MPC-HC         | Same as before            | Same as before | Same as before | Same as before | User        |
| Sample ABC - Firefox        | Same as before            | Same as before | Same as before | Same as before | User        |
| Sample XYZ - Windows News   | Same as before            | Same as before | Same as before | Same as before | User        |
| Microsoft Photos            | IDWTFR - Microsoft Photos | Junk Rules     | Block          | Disable        | Not by user |
| App Installer               | IDWTFR - App Installer    | Junk Rules     | Block          | Disable        | Not by user |
| Feedback Hub                | IDWTFR - Feedback Hub     | Junk Rules     | Block          | Disable        | Not by user |
| Microsoft Edge              | IDWTFR - Microsoft Edge   | Junk Rules     | Block          | Disable        | Not by user |
+-----------------------------+---------------------------+----------------+----------------+----------------+-------------+


I'm new to PowerShell, so any help will be appreciated. Thanks.

1 Answers

Since this is your special use case, it's going to a challenge for one to validate without setting up an environment as close as possible to what you show here. I am in no position to do that.

Yet, looking at what you say you have done, here is a refactor option to try. Refactor a bit (again, not tested)

# Get all firewall rule name, and filter out the named rules
Get-NetFirewallRule | 
Where-Object -Property Name -notlike 'Sample ABC|Sample XYZ' | 
ForEach {
    # Disable all other rules that are not added by the user
    Set-NetFirewallRule -Name $PSItem.DisplayGroup -Enabled False

    # Set rules' action to block
    $PSItem.DisplayName | 
    Set-NetFirewallRule -Action Block

    # Rename firewall rule
    If ($PSItem.DisplayName -like '*Microsoft*')
    {Rename-NetFirewallRule -Name $PSItem.DisplayName -NewName "IDWTFR-$($PSitem.DisplayName)"}

    # Create new firewall group
    $PSItem.Group = 'JunkRules' | 
    Set-NetFirewallRule -NewDisplayName $PSItem.DisplayName
}
Related