-
-
Save oznotes/d9c7572ae1d157583d3c68ecdd473581 to your computer and use it in GitHub Desktop.
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(); | |
} | |
} | |
} | |
} |
hi, i get syntactic errors in, using System.Management.Automation; and in using (PowerShell PowerShellInst = PowerShell.Create ())
use visual studio 2022
How do I select the folders that I want to exclude?
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
Thanks for your help, and how could I make it exclude the desired folder (s)?
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
I have installed nuget package System.Management.Automation but it doesn't exclude the folder either
thanks for the help i already got it working
Sorry to bother you again, is there a way to hide the cmd window during execution?
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();
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.
must run as administrator !
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.
me sirvió mucho gracias :)