Skip to content
Menu

Defender Turned Off or Broken - Switch It Back On

Repairs Microsoft Defender when it is the PC's antivirus and is off or broken: clears local disabling policy, starts services, turns protection on and updates signatures.

Defender turned off or broken - switch it back on is free for members.

Enter your email to create your free account and get the file.

We'll email you the script and the weekly MSP Outpost email. Unsubscribe anytime. Privacy policy.

Already a member? Sign in.

Be the first to review Defender turned off or broken - switch it back on. Tell other MSPs how it worked for you.

Write a Review

LanguagePowerShell
Runs onWindows 10, Windows 11, Windows Server 2016+; runs as SYSTEM
NeedsPowerShell 5.1; Microsoft Defender installed; administrator rights
Changes thingsChanges what it runs on

What It Does

The script repairs Microsoft Defender Antivirus when it is the antivirus this PC relies on and it is off or broken. It removes policy values that turn Defender off from HKLM\SOFTWARE\Policies\Microsoft\Windows Defender: DisableAntiSpyware, DisableAntiVirus and the real-time protection values. It does so only when the value is 1 and no Group Policy file, local or domain, mentions it. A value Group Policy sets is reported as managed by Group Policy and left alone, so change the policy instead. The policy key is exported to the MSPOutpost backup folder first.

It sets the WinDefend service to Automatic when it is Disabled, starts WinDefend when stopped or previously disabled, and then tries to start WdNisSvc if it exists, is stopped and is enabled, turns real-time, behavior and download scanning back on with Set-MpPreference, and updates signatures with Update-MpSignature when UpdateSignatures is true. If no repair is needed, it leaves the PC alone. A failed signature update is a warning, not an error.

It is skipped when another antivirus is active in Windows Security Center on a workstation, or when Defender is in passive mode. On a server it does nothing unless Force is true, because turning Defender back on would run two engines on a production server. Tamper protection stops Windows accepting some of these changes, and the script says so if Defender is still not protecting afterwards. It does not read Intune policy, remove other antivirus products or touch exclusions.

Before You Run It

  • Run it with DryRun=true first and read what it would remove, start or turn on.
  • Check that Defender is meant to be the antivirus here. It skips a PC with another active antivirus.
  • If it reports managed by Group Policy, fix the policy rather than the PC.
  • If tamper protection is on and Defender is still off afterwards, turn tamper protection off in your security console first.

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: Removes local policy values that disable Defender (backed up first); sets WinDefend to Automatic and starts it; turns on real-time protection; updates signatures.
  • Restart: It never restarts the machine.
  • Typical run: Under 2 minutes.
  • Example answer: "Defender repaired: removed 2 policy values, started WinDefend, real-time protection on, signatures updated"
  • 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.

  • UpdateSignatures: true (default) to run Update-MpSignature after repairing; a failed update is a warning, not an error.
  • Force: false (default): on a server the script does nothing and says so, because a third-party antivirus usually switches Defender off there with DisableAntiSpyware=1, and turning Defender back on would run two engines on a production server. true to repair Defender on servers anyway.

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.

The Script

repair-defender.ps1 · 981 lines · 50.7 KB

Free for Members

Create a free account or sign in to read, copy and download this script.

Welcome Back

Sign in to your MSP Outpost account.