Search code examples
c++visual-studiowindows-defender

Why is Windows Defender scanning my code?


I am compiling C++ code using Visual Studio 2019. The code ranges from simple projects to learn C++ features to game dev. But no matter the scope of the projects I get this message:

Security Scan Required Your administrator requires a security scan of this item. The scan could take up to 10 seconds.

This stalls the Visual C++ compiler and ultimately slows down my workflow. This occurs every time I try to compile my code. The weird thing is, on this same machine I've been compiling code without this problem. I am unsure of what caused this change and I want to revert it but don't know how.


Solution

  • The problem here occurs due to Windows Defender Real-Time Protection, which is very important to keep the system safe. The solution I found was based on these articles:

    Microsoft description of the problem and simple fixes

    How to create and run Windows Powershell scripts

    Powershell script that excludes all the necessary directories and processes for Visual Studio 2019

    The solution is to add Windows Defender Exclusions to all the processes and directories you use for your development environment, this gets rather complicated to do on your own and differs depending on your development environment of choice, here I am using Visual Studio 2019 and will use a Powershell script to quickly add these exclusions.

    Simply download the Powershell script, open Windows Powershell, and type Set-ExecutionPolicy Unrestricted

    Then navigate to the directory of the script. For simplicity, put the script in your root C: and navigate there then type ./{Script name} the "./" are important to run scripts from non-default directories.

    The script will ask where you store your project folders, this is important to exclude from Windows Defender. After running the script you're pretty much done.

    One final step, Set-ExecutionPolicy Restricted and then Get-ExecutionPolicy to verify it's restricted, it's very important not to overlook your Powershell Execution Policy as it's a very powerful attack vector.

    I have done these exact steps and can verify that Windows Defender no longer stalls my code builds using Visual Studio 2019. This might mean that running an older or newer version of Visual Studio with a different directory structure would require another script, or Microsoft could step in and create a universal solution to exclude their own development environments form Windows Defender scans.