How to Run PowerShell from C#

0
282
How to Run PowerShell from C#

To run PowerShell from C#, you can use the System.Management.Automation namespace, which provides a set of classes for executing and managing Windows PowerShell.

The System.Management.Automation namespace is a .NET namespace that provides a set of classes for executing and managing Windows PowerShell. It allows you to run PowerShell scripts and commands from a .NET application, such as a C# program.

Using the System.Management.Automation namespace, you can create PowerShell object and add scripts or commands to it. You can then execute the scripts or commands by calling the Invoke method. You can also pass arguments to the scripts or commands using the AddArgument method.

More: Debugging and Error Handling in C#

Here’s an example of how you can use this namespace to run a PowerShell script from a C# program:

using System.Management.Automation;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PowerShell object
            PowerShell ps = PowerShell.Create();

            // Add a script to the PowerShell object
            ps.AddScript("Get-Process | Where-Object {$_.ProcessName -like 'chrome'} | Stop-Process");

            // Execute the script
            ps.Invoke();
        }
    }
}

This example creates a PowerShell object and adds a script to it that gets a list of processes with the name “chrome” and stops them. The script is then executed by calling the Invoke method.

You can also pass arguments to the script by using the AddArgument method before calling Invoke. For example:

ps.AddArgument("arg1");
ps.AddArgument("arg2");
ps.Invoke();

This would pass the arguments “arg1” and “arg2” to the script when it is executed.

Run PowerShell from C# – Runspace Method

The Runspace class in the System.Management.Automation namespace provides a method called CreatePipeline that you can use to create a pipeline within a runspace. A pipeline is a series of commands that are executed in sequence, with the output of each command being passed as input to the next command.

Here’s an example of how you can use the Runspace class and the CreatePipeline method to run a PowerShell script from a C# program:

using System.Management.Automation;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a runspace
            Runspace runspace = RunspaceFactory.CreateRunspace();

            // Open the runspace
            runspace.Open();

            // Create a pipeline
            Pipeline pipeline = runspace.CreatePipeline();

            // Add a script to the pipeline
            pipeline.Commands.AddScript("Get-Process | Where-Object {$_.ProcessName -like 'chrome'} | Stop-Process");

            // Execute the pipeline
            pipeline.Invoke();

            // Close the runspace
            runspace.Close();
        }
    }
}

This example creates a runspace, opens it, creates a pipeline within the runspace, adds a script to the pipeline, and then executes the pipeline. Finally, the runspace is closed.

Using the CreatePipeline method allows you to create multiple pipelines within a single runspace, which can be useful if you want to execute multiple commands or scripts concurrently.

I hope this helps! Let me know if you have any questions.