Skip to content

Instantly share code, notes, and snippets.

@oznotes
Last active August 29, 2023 16:54
Show Gist options
  • Save oznotes/d9c7572ae1d157583d3c68ecdd473581 to your computer and use it in GitHub Desktop.
Save oznotes/d9c7572ae1d157583d3c68ecdd473581 to your computer and use it in GitHub Desktop.
Add Folder to Defender Exclusion C# .NET
using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Management.Automation;
// Add Folder to Defender Exclusion running powershell script with-in C# .NET
namespace helper
{
class Program
{
// run the software with administrator rights
// app.manifest
// <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
static void Main()
{
using (PowerShell PowerShellInst = PowerShell.Create())
{
// Get the Folder Path.
string path = Directory.GetCurrentDirectory();
PowerShellInst.AddScript(@"Add-MpPreference -ExclusionPath '" + path + "'");
Collection<PSObject> PSOutput = PowerShellInst.Invoke();
Console.WriteLine(path+" to Windows Defender [added][+]");
Console.Read();
}
}
}
}
@zk-error
Copy link

me sirvió mucho gracias :)

@javitovalencia
Copy link

hi, i get syntactic errors in, using System.Management.Automation; and in using (PowerShell PowerShellInst = PowerShell.Create ())
use visual studio 2022

@javitovalencia
Copy link

How do I select the folders that I want to exclude?

@oznotes
Copy link
Author

oznotes commented Jan 3, 2022

How do I select the folders that I want to exclude?

Just run the software inside the folder you want to add excluded Folder

for the using System.Management.Automation

Try to check Nuget packages and install the correct one

@javitovalencia
Copy link

Thanks for your help, and how could I make it exclude the desired folder (s)?

@javitovalencia
Copy link

nuget package cake.powershell is the only one that asks but it still doesn't work, after compiling and running as administrator it doesn't work for me

@javitovalencia
Copy link

I have installed nuget package System.Management.Automation but it doesn't exclude the folder either

@javitovalencia
Copy link

thanks for the help i already got it working

@javitovalencia
Copy link

Sorry to bother you again, is there a way to hide the cmd window during execution?

@oznotes
Copy link
Author

oznotes commented Dec 10, 2022

Sorry to bother you again, is there a way to hide the cmd window during execution?

Try commenting out these two lines

                //Console.WriteLine(path+" to Windows Defender [added][+]");
                //Console.Read();

@javitovalencia
Copy link

Sorry to bother you again, is there a way to hide the cmd window during execution?

Try commenting out these two lines

                //Console.WriteLine(path+" to Windows Defender [added][+]");
                //Console.Read();

Thanks for your help, the black cmd window still appears for a short time, but it's much better.

@AhmedK97
Copy link

must run as administrator !

@oznotes
Copy link
Author

oznotes commented Aug 29, 2023

Thanks for your help, the black cmd window still appears for a short time, but it's much better.

  • Open the properties of your project by right-clicking on your project in the Solution Explorer and selecting 'Properties'.
  • Go to the 'Application' tab.
  • Change the 'Output type' dropdown to 'Windows Application'.

This will prevent a console window from showing up, but it will still allow your application to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment