title: Calling PowerShell From C#
body: <p>I am using <code>System.Management.Automation</code> DLL which allows me to call PowerShell within my C# application like so:</p>
  
  <pre><code>PowerShell.Create().AddScript("Get-Process").Invoke();
  </code></pre>
  
  <p>What I am trying to do is call PowerShell but supply the input list.  For example, in:</p>
  
  <pre><code>1, 2, 3 | ForEach-Object { $_ * 2 }
  </code></pre>
  
  <p>I am trying to supply the left hand side <code>1, 2, 3</code> when invoking:</p>
  
  <pre><code>// powershell is a PowerShell Object
  powershell.Invoke(new [] { 1, 2, 3 });
  </code></pre>
  
  <p>However this does not work.  The <b>workaround</b> I came up with was using <code>ForEach-Object</code> and then passing the array as an <code>InputObject</code> with the <code>{ $_ }</code> as the <code>Process</code>:</p>
  
  <pre><code>// create powershell object
  var powershell = PowerShell.Create();
  
  // input array 1, 2, 3
  Command inputCmd = new Command("ForEach-Object");
  inputCmd.Parameters.Add("InputObject", new [] { 1, 2, 3 });
  inputCmd.Parameters.Add("Process", ScriptBlock.Create("$_"));
  powershell.Commands.AddCommand(inputCmd);
  
  // ForEach-Object { $_ * 2 }
  Command outputCmd = new Command("ForEach-Object");
  outputCmd.Parameters.Add("Process", ScriptBlock.Create("$_ * 2"));
  powershell.Commands.AddCommand(outputCmd);
  
  // invoke
  var result = powershell.Invoke();
  </code></pre>
  
  <p>Although the above is working code is there any way of using <code>Invoke</code> passing in the input array because I would have though that this would be desirable way of calling it?</p>