Как стать автором
Обновить

Powershell way to exclude folder from Defender list

Время на прочтение2 мин
Количество просмотров4K
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

Теги:
Хабы:
Рейтинг0
Комментарии0

Публикации

Истории

Работа

Ближайшие события

27 августа – 7 октября
Премия digital-кейсов «Проксима»
МоскваОнлайн
11 сентября
Митап по BigData от Честного ЗНАКа
Санкт-ПетербургОнлайн
14 сентября
Конференция Practical ML Conf
МоскваОнлайн
19 сентября
CDI Conf 2024
Москва
24 сентября
Конференция Fin.Bot 2024
МоскваОнлайн
25 сентября
Конференция Yandex Scale 2024
МоскваОнлайн
28 – 29 сентября
Конференция E-CODE
МоскваОнлайн
28 сентября – 5 октября
О! Хакатон
Онлайн
30 сентября – 1 октября
Конференция фронтенд-разработчиков FrontendConf 2024
МоскваОнлайн