Skip to content

Instantly share code, notes, and snippets.

@SradnickDev
Last active July 5, 2020 17:13
Show Gist options
  • Save SradnickDev/a2b566e9c149b0f60d980913fb09e31f to your computer and use it in GitHub Desktop.
Save SradnickDev/a2b566e9c149b0f60d980913fb09e31f to your computer and use it in GitHub Desktop.
Powershell script that apply resharper recommendations and exclude certain files and folders from windows defender.
[Collections.ArrayList]$vsFolders = @()
[Collections.ArrayList]$excludedFolders = @("$env:LOCALAPPDATA\JetBrains\Transient")
[Collections.ArrayList]$excludedFiles = @()
$vsVersions = @('2017', '2018', '2019')
Function Invoke-AsAdmin()
{
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator'))
{
Start-Process -FilePath powershell.exe -ArgumentList ("-NoProfile -ExecutionPolicy Bypass -File `"{0}`"" -f $PSCommandPath) -Verb RunAs
exit
}
}
Function Find-VS
{
param
(
[Parameter(Mandatory = $true)]
$InstalledSoftware
)
$location = 'InstallLocation'
foreach($obj in $InstalledSoftware)
{
$softwareName = $obj.GetValue('DisplayName')
foreach($version in $vsVersions)
{
$target = 'Visual Studio Community {0}' -f $version
if($target -eq $softwareName)
{
$null = $excludedFolders.Add($obj.GetValue($location))
$null = $vsFolders.Add($obj.GetValue($location))
}
}
}
}
Function Find-Processes()
{
foreach($path in $vsFolders)
{
$file = Get-ChildItem -Path $path -Filter msbuild.exe -Include *.exe -Recurse -ErrorAction SilentlyContinue -Force
$null = $excludedFiles.Add($file)
$file = Get-ChildItem -Path $path -Filter devenv.exe -Include *.exe -Recurse -ErrorAction SilentlyContinue -Force
$null = $excludedFiles.Add($file)
}
}
Function Select-FOlderDialog
{
[CmdletBinding()]
param
(
[string]
$initialDirectory = 'desktop'
)
$null = Add-Type -AssemblyName System.Windows.Forms
$folderDialog = New-Object -TypeName System.Windows.Forms.FolderBrowserDialog
$folderDialog.Description = 'Select a folder/project to exclude from windows defender!'
$folderDialog.rootfolder = 'MyComputer'
$folderDialog.SelectedPath = $initialDirectory
$result = $folderDialog.ShowDialog((New-Object -TypeName System.Windows.Forms.Form -Property @{
TopMost = $true
TopLevel = $true
}))
if($result -eq 'OK')
{
$folder += $folderDialog.SelectedPath
}
return $folder
}
Function Add-Folders()
{
$newFolder = Select-FOlderDialog
if($newFolder -eq $null)
{
return
}
$null = $excludedFolders.Add($newFolder)
$result = Read-Host -Prompt 'add another folder ? (y/n)'
if($result -eq 'y')
{
Select-FOlderDialog
}
}
Function Set-Exclusions()
{
Write-Output -InputObject 'exclude from windows defender'
foreach($folder in $excludedFolders)
{
Add-MpPreference -ExclusionPath $folder
Write-Output -InputObject $folder
}
foreach($file in $excludedFiles)
{
Add-MpPreference -ExclusionProcess $file
Write-Output -InputObject $file | Select-Object -Property Name -ExpandProperty Name
}
}
Invoke-AsAdmin
Find-VS -InstalledSoftware (Get-ChildItem -Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall')
Find-VS -InstalledSoftware (Get-ChildItem -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall')
Find-Processes
Write-Output 'Add folder to exclude.'
Add-Folders
Set-Exclusions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment