What It Does
The script reads the Domain, Private and Public profiles twice: as they are in force, which includes Group Policy and MDM, and as they are set on this PC. For each profile that is off, or that allows inbound connections by default, it saves the firewall policy as a registry export and a netsh export you can restore with netsh advfirewall import, turns the profile on, and sets its default inbound action to Block when BlockInbound is true. It starts the Windows Defender Firewall service when it is stopped.
It changes the local settings only. When Group Policy or MDM sets a value that is not what is wanted, that profile is reported as managed by Group Policy and left alone. It never adds, edits or deletes a firewall rule and never changes outbound defaults. Turning inbound Block back on does not remove any Allow rule, so enabled rules such as remote desktop stay enabled.
It is skipped on servers unless AllowServers is true, and when Windows Security Center lists an enabled firewall product that is not Microsoft's. It does not look at third-party firewall products.
Before You Run It
- Run it with DryRun=true first and read which profiles it would change.
- Do not use AllowServers on a server that ran with the firewall off until you have checked its open ports. Blocking inbound can cut off SQL and line-of-business ports.
- If a profile is reported as managed by Group Policy, change the policy instead.
- Use BlockInbound=false if you only want the profiles turned on.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Turns Windows Firewall profiles on and sets default inbound to Block (local settings only); starts the firewall service; saves a backup of the firewall policy first.
- Restart: It never restarts the machine.
- Typical run: Under 30 seconds.
- Example answer: "Public: turned on; Private: default inbound set to Block; Domain managed by Group Policy"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- BlockInbound: true (default) to also set the default inbound action of each profile to Block; false to only turn the profiles on.
- AllowServers: false (default): on a server the script does nothing and says so, because blocking inbound on a server that ran with the firewall off can cut off SQL and line-of-business ports. true to act on servers too (check the open ports first).
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.