What It Does
The script applies a fixed set of 14 Microsoft Defender attack surface reduction rules, the three standard protection rules plus the other rules Microsoft recommends for most PCs, to Audit or Block mode with Add-MpPreference. It reports each rule before and after. The list is written out in the script with names.
Audit, the default, logs what a rule set to Audit would have blocked. A rule already in Block or Warn keeps its existing behavior. Run in Audit first, read the events, then run again with Mode Block. Block stops the behavior, and Office macro and script rules can stop line-of-business software that relies on them, so audit first.
A rule that is already at or stricter than the wanted mode is left alone, so Audit never lowers a rule that is on Block or Warn. ExcludeRules lists rule IDs to leave exactly as they are. The rule that blocks process creations from PSExec and WMI commands is not in the set because RMM tools and software deployment use those paths.
It is skipped when Microsoft Defender is not the active antivirus. If Defender is off, the script skips a server and stops with an error on a workstation where no other antivirus is active. Rules pushed by Intune or Group Policy win over this script, and if one does not take the run ends in an error naming it. It never touches exclusions, Controlled Folder Access or any other Defender setting.
Before You Run It
- Start in Audit mode and read the events for a few weeks before you move to Block.
- Find the line-of-business software that uses Office macros or scripts, and test it in Block.
- Use ExcludeRules for any rule that breaks a client's software.
- Run repair-defender first if Defender is off.
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: Sets Defender attack surface reduction rules to Audit or Block (Add-MpPreference); saves a registry backup of the ASR settings first.
- Restart: It never restarts the machine.
- Typical run: Under 1 minute.
- Example answer: "ASR rules set to Audit: 11 changed, 3 already set"
- 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.
- Mode: Audit (default) or Block.
- ExcludeRules: Rule IDs (GUIDs from the list in this script) to leave unchanged, separated by commas. Default none.
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.