Windows Defender, the built-in anti-virus program in Windows, has useful features such as cloud-based protection, offline scanning, limited periodic scanning, tamper protection, controlled folder access, etc.
Almost every aspect of Windows Defender can be managed or automated using the MpCmdrun.exe command-line tool and PowerShell cmdlets. There are times when you need to temporarily disable your Windows Defender real-time protection on your test (non-production) systems and switch it back on after a few minutes.
You may prefer a single-click shortcut or script solution because of the following reasons:
- It takes several mouse clicks to turn off/on the Windows Defender real-time protection through the user interface.
- The Turn off Microsoft Defender Antivirus Group Policy setting or its equivalent registry setting
DisableAntiSpyware = 1would require rebooting the computer.
- Microsoft has discontinued the
DisableAntiSpywarepolicy/registry setting in Microsoft Defender Antimalware platform versions 4.18.2007.8 and higher.
DisableAntiSpywareno longer works!
Enable or disable Windows Defender using Shortcut or Command-line
I’ll do more testing and see if a workaround is available.
This method turns off the Microsoft (Windows) Defender service altogether, which means every component of Windows Defender (including the real-time protection, Microsoft Defender Antivirus Network Inspection Service, cloud-based protection, limited periodic scanning, tamper protection, controlled folder access, etc.,)
This method works even if the Tamper Protection setting is enabled in the Windows Defender user interface. The Microsoft Defender Antivirus Service can be turned off only by a process running under the
- Download AdvancedRun from the following page at Nirsoft’s site.
- Extract the executable
AdvancedRun.exeto a permanent folder — let’s say
- Create a Windows script file containing the following lines of code. To create a script file (.vbs), use Notepad.
'Description: Script to disable the Microsoft Defender Antivirus service Set ServiceSet = GetObject("winmgmts:").ExecQuery _ ("select * from Win32_Service where Name='WinDefend'") For Each Service In ServiceSet RetVal = Service.StopService() If RetVal <> 0 Then MsgBox "Error " & RetVal End If Service.ChangeStartMode("Manual") Next
- Save the script file as
- From the Run dialog, run the following command-line to disable Windows Defender altogether:
D:\Tools\AdvancedRun.exe /EXEFilename "%windir%\system32\wscript.exe" /CommandLine '"D:\Tools\disable-defender.vbs"' /RunAs 8 /Run
(Optionally, you can create a desktop shortcut to the above command.)
That disables Microsoft Defender Antivirus Service & Microsoft Defender Antivirus Network Inspection Service.
You may also see the following message when opening the Windows Security “Security at a glance” page.
Page not available Your IT administrator has limited access to some areas of this app, and the item you tried to access is not available. Contact IT helpdesk for more information.
Editor’s note: If your PC is used by multiple users, you also need to make sure that the script file is saved in a secure location so that it can’t be tampered with by other users. Use NTFS permissions accordingly to secure the file.
How to Enable and Start Microsoft Defender?
To enable and start Microsoft Defender Antivirus Service and Microsoft Defender Antivirus Network Inspection Service back, follow these steps:
Create another script file named
enable-defender.vbs with the following contents:
'Description: Script to enable the Microsoft Defender Antivirus service Set ServiceSet = GetObject("winmgmts:").ExecQuery _ ("select * from Win32_Service where Name='WinDefend'") For Each Service In ServiceSet Service.ChangeStartMode("Automatic") RetVal = Service.StartService() If RetVal <> 0 Then MsgBox "Error " & RetVal End If Next
Then launch the script as TrustedInstaller, using the following AdvancedRun command-line:
D:\Tools\AdvancedRun.exe /EXEFilename "%windir%\system32\wscript.exe" /CommandLine '"D:\Tools\enable-defender.vbs"' /RunAs 8 /Run
You can create separate desktop shortcuts to the above commands to quickly enable or disable the Microsoft Defender Antivirus service.
(An alternative tool/method to enable/disable Microsoft Defender is Defender Control app from Sordum.)
You can disable Windows Defender real-time protection using this PowerShell command:
powershell.exe -command "Set-MpPreference -DisableRealtimeMonitoring $true"
After you run the above command from an elevated Command Prompt or Run dialog (elevated), it disables the real-time protection component of Windows Defender. Following that, you’ll immediately see the “Virus & threat protection” action center notification.
And to enable the real-time protection back, use this command-line:
powershell.exe -command "Set-MpPreference -DisableRealtimeMonitoring $false"
The above commands need to be run elevated (run as administrator).
This method uses a simple PowerShell script, which, when run, toggles the Windows Defender real-time protection setting. If it’s turned off, the script turns it back on, and vice versa. So, you’ll need just one shortcut if using this method.
- Copy the following lines to Notepad:
$preferences = Get-MpPreference Set-MpPreference -DisableRealtimeMonitoring (!$preferences.DisableRealtimeMonitoring)
- Save the file with a
.ps1extension in a permanent location. Let’s say
- Create a desktop shortcut with the following command:
powershell.exe -ExecutionPolicy Bypass -File "D:\Tools\defender-realtime-toggle.ps1"
That’s it! As always, whenever you plan to run the shortcut/script, you need to run it elevated (run as administrator.)
Other PowerShell cmdlets to manage Windows Defender
To know the complete list of PowerShell cmdlets for managing Windows Defender, check out the Microsoft docs article on Defender-specific PowerShell cmdlets.
We’ve seen PowerShell’s Defender-specific cmdlets earlier in our earlier articles mentioned below:
I hope you liked the shortcut or command-line method to enable and disable Windows Defender real-time protection on your computer.
(This article was last updated on April 7, 2022. Tested on Windows 10 Version 21H2 Build 19044.1466 and Windows 11 Version 21H2 Build 22000.438. The steps listed under “Method 1” don’t work on Windows 10 21H2 Build 10.0.19044.1586. The script throws up “Error 2”.)