'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 1:[1]

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.

Solution 2:[2]

Go to

Start > Settings > Update & Security > Windows Security > Virus & threat protection 

Under Virus & threat protection settings, select Manage settings, and then under Exclusions, select Add or remove exclusions. Select Add an exclusion, and then select the file type .cpp from files, folders, file types, or process.

Add an exclusion to Windows Security

Solution 3:[3]

Okay, I also got this message when I was using CodeRunner Extension in VSCode. Every time I run the code with CodeRunner I was getting this Notification.

The solution that worked for me was simple:

  1. Go To Virus & Threath Protection.
  2. Turn Off these Two Options: Cloud-delivered protection, Automatic sample submission. Just like this:

enter image description here

Solution 4:[4]

The answer that OP has marked accepted worked. However, the script he mentioned has to be something written on it, so I created a new script (See the 2nd link mentioned in the above post for creating a script) and copied the code from Github https://gist.github.com/dknoodle/5a66b8b8a3f2243f4ca5c855b323cb7b and pasted in my script and run the command ./{Name of your script} in the Powershell. Be sure to open PowerShell in Administrator mode. Also if you are pasting your script in root C: and want to open your script then make sure that your command refers to the root folder of C: .You can type cd.. to come out of the directory.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AbdelRahman Mahmoud
Solution 2 Anurag Dabas
Solution 3 Nishant Bhagat
Solution 4 ADITYA KUMAR