I am using System.Management.Automation
DLL which allows me to call PowerShell within my C# application like so:
PowerShell.Create().AddScript(\
A little bit late but:
PowerShell ps = PowerShell.Create();
ps.Runspace.SessionStateProxy.SetVariable("a", new int[] { 1, 2, 3 });
ps.AddScript("$a");
ps.AddCommand("foreach-object");
ps.AddParameter("process", ScriptBlock.Create("$_ * 2"));
Collection results = ps.Invoke();
foreach (PSObject result in results)
{
Console.WriteLine(result);
}
returns:
2
4
6