Share via


How do I disable Windows Defender ("WinDefend") service?

Question

Sunday, February 12, 2017 2:44 PM

Hello guys,
I would like to disable Windows Defender ("WinDefend") service using C # code.
I have tried something like the code below, but this does not work. Please for assistance. Thank you.

private void disabledefender()
        {           
                    RunCmd("/c net stop WinDefend");
                    ProcStartargs("powershell", "-command \"Set-Service -Name WinDefend -StartupType Disabled\"");
                    _OutPut("Windows Defender disabled");       
               Registry.SetValue("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\WinDefend", "Start", 4);          
        }

All replies (4)

Sunday, February 12, 2017 2:54 PM ✅Answered

You could try using the ServiceController class to set the StartType to Disabled:

https://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.starttype(v=vs.110).asp

You will need to run the program as an Administrator, otherwise it will not be sufficiently privileged to change the service (beware of UAC!). You can use the app.manifest to set the requestedExecutionLevel to RequireAdministrator.


Monday, February 13, 2017 11:00 AM ✅Answered

You can do the same thing which is done manually with gpedit.msc =>

with IGroupPolicyObject interface, by setting the value "DisableAntiSpyware"  in "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender"

(tested on Windows 10)


Monday, February 13, 2017 4:44 PM ✅Answered

Thanks for the replies.
Of course the program to run as administrator.
Of course I use System.ServiceProcess - ServiceController.
I tried many ways to Stop service "WinDefend", but it was not possible to make such a code ...I do not know what the problem is.

It is because myService.Stop(); calls OpenServiceW() with SERVICE_STOP and it returns ERROR_ACCESS_DENIED for "WinDefend" (even with Task Manager on my PC) **
**

(but no error with IGroupPolicyObject on the same machine**)**


Monday, February 13, 2017 3:32 PM

Thanks for the replies.
Of course the program to run as administrator.
Of course I use System.ServiceProcess - ServiceController.
I tried many ways to Stop service "WinDefend", but it was not possible to make such a code ...I do not know what the problem is.

This code which I have established above in my first post works only after restarting the computer (Windows 10).