Running PowerShell from C#


using System.Collections.ObjectModel;
using System.Management.Automation;
using static System.Console;

namespace InvokePS
{
class Program
{
static void Main(string[] args)
{
string message = "'does this work?'";
string script = $@"C:\Users\joe.bothwell\Desktop\HelloWorld.ps1 -a {message}";
PowerShell shell = PowerShell.Create();
shell.Commands.AddScript(script);

Collection results = shell.Invoke();

foreach(PSObject result in results)
{
WriteLine(result.ToString());
}

WriteLine("Press any key to continue...");
ReadLine();
}
}
}


Comments (0)

Skip to main content