Visual Studio Performance with Microsoft Defender

Published on Thursday, October 27, 2022

Antivirus Exclusion Developer Dall-E image

Steve Smith posted about speeding up built times in Visual Studio by configuring Windows Defender. That was in 2016 and to say things have changed a bit is probably an understatement. Configuring a new laptop, I thought I'd revisit this briefly.

Before changing anything in Windows Virus & Threat Protection, go ahead and run a scan to make sure we're starting with a clean slate. Go to Windows Security and click Virus & threat protection then click the Quick scan button. I've been advocating scripting all-the-things, to run a quick scan in an administrator Powershell terminal run Start-MpScan -ScanType QuickScan. You can also run a full-scan, if that makes you more comfortable: Start-MpScan -ScanType FullScan.

Once that's complete we can configure Windows Virus and Threat Protection to "trust" (exclude) Visual Studio. To do that in PowerShell you can use the App-MpPreference cmdlet (as well as see what's already configured with the Get-MpPreference cmdlet). Some examples:

With Visual Studio 2022 Enterprise:

Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\devenv.exe"

With Visual Studio 2022 Professional:

Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\Microsoft Visual Studio\2022\Professional\Common7\IDE\devenv.exe"

With Visual Studio 2022 Community:

Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\Microsoft Visual Studio\2022\Community\Common7\IDE\devenv.exe"

And, with Visual Studio 2022 Preview:

Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe"

You can also do that for Visual Studio Code:

Add-MpPreference -ExclusionProcess "$Env:LocalAppData\Programs\Microsoft VS Code\code.exe"

You can also exclude the location of where you store your source code. The default location is C:\Users\<user-name>\source\repos for Visual Studio. So, in PowerShell, you can add a path exclusion:

Add-MpPreference -ExclusionPath "$Env:USERPROFILE\source\repos"
If you're working with Git repositories that you're unsure of what they contain, you may want to separate where you clone those repos from where you exclude.

Or, if you want a PowerShell script to just do all thee things, see optimize-defender.ps1

Other processes to consider:

Add-MpPreference -ExclusionProcess "$Env:ProgramFiles\dotnet\dotnet.exe"

Any other processes or paths that you'd consider for exclusion?

comments powered by Disqus