\

# How to run PowersShell script in c#

I will show you how to run a PowerShell script in c#.

The execution method in this article is to call Powershell.exe instead of using System.Management.Automation.

## Source code

The following code creates the command string that follows PowerShell.exe.

-File option specifies the PowerShell script file path.

Loop through args using foreach to create a command string that specifies the arguments.

Create a process object and specify the command string.

Specifies to hide the window, etc.

Start the process and get standard output, standard error, and exit code.

## PowerShellArgs Class

Use the following class for the argument passed to the PowerShell script.
In the function explained above, considering that multiple specifications are specified, it is passed as a list type.

## How to run

For example, suppose you run a PowerShell script like this:

This scirpt opens the file specified by OpenFileNamearguments with Visual Studio Code.

The code to call the ExecPSProcess function is below.

Specify the PowerShell script file path to execute and define the arguments.

Arguments are stored in a list type PowerShellArgs object.

Define a variable to get the standard output, standard error, and exit code.

Finally, call ExecPSProcess.