7 May

Powershell way to exclude folder from Defender list

PowerShellDevelopment for Windows
To exclude some folder from the defender list can be used cmdlet Set-MpPreference
Manly, now you can stop reading, go to the PowerShell and update excludes:)
But if you are want to see how to perform this in more detail — welcome under the habrcut.

UPD: Added a script to add selected folder to excluded Defender locations (at the end of the post)

For the beginning, let's go through the current excluded folders list.

To perform this operation we need to open the PowerShell session with admin rights and execute next commands

$defenderSettings = Get-MpPreference
$defenderSettings.ExclusionPath

image

As you see Rider and Pycharm already added some folders on my machine

Let's remove the specified repositories folders and make the rule more generic:

image

Line by line:

$currentItems=@($defenderSettings.ExclusionPath)

Store our folders list to a new variable

$currentItems

Asserting the list is copied.

$newItems = $currentItems -notlike "C:\Users\sync\Pycharm"
$newItems = $newItems -notlike "C:\Users\sync\Rider"
$newItems = $newItems -notlike "D:\Sources\B*"

Removes «C:\Users\sync\Pycharm» «C:\Users\sync\Rider» and «D:\Sources\B*» folders from the list. You probably will have some another pattern.

$newItems

Verifying the resulting list.

As a more generic folders filtering could be used something like this

$newItems = $currentItems -notlike "C:\Users*\Pycharm"

but double check before moving to the next step.

Adding new folders to the excluded list:

As we are removing the paths added by rider and Pycharm the environment can start working slower. So it's the time to add the folders with our sources.

image

$updatedList=$newItems
$updatedList += "C:\Users\sync\PycharmProjects\"
$updatedList += "C:\Users\sync\RiderProjects\"
$updatedList += "D:\Sources\"
$updatedList += "C:\Users\sync\source\repos"
$updatedList

The first 2 lines intended to add Rider and Pycharm projects.
Then adding our additional source folders path.
And in the latest command just verifying the resulting list.

And now time to save new folders to defender exclude list:

image

The first command just to double-check the folders collection.

Store our changed exclude list to Defender:

Set-MpPreference -ExclusionPath $updatedList

And verifying the result:

Get-MpPreference | %{$_.ExclusionPath}

Perfest! Defender will never cause again the IDE freezes

Bonus! Script to add current folder to excludes list:


$defenderSettings = Get-MpPreference
$currentItems=@($defenderSettings.ExclusionPath)
$currentItems +=$pwd.Path
$currentItems
Set-MpPreference -ExclusionPath $currentItems

Now you can save this code under the project folder and ship with your sources.

Demonstation of work

Tags:powershelldefenderwindows101
Hubs: PowerShell Development for Windows
0
131 0
Leave a comment
Top of the last 24 hours