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();
}
}
}
}
@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