title: How to pass arguments to PowerShell via C#
body: <p>I having issues with passing arguments to PowerShell via C#</p>
  
  <p>I am getting the following exception:</p>
  
  <blockquote>
    <p>"A command that prompts the user failed because the host program or
    the command type does not support user interaction. Try a host program
    that supports user interaction, such as the Windows PowerShell Console
    or Windows PowerShell ISE, and remove prompt-related commands from
    command types that do not support user interaction, such as Windows
    PowerShell workflows"</p>
  </blockquote>
  
  <p>cs:</p>
  
  <pre><code>private static void RunPowershellScript(string scriptFile, string scriptParameters)
  {
      string scriptParameters = "param1 param2";
  
      RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
      Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
      runspace.Open();
      RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
      Pipeline pipeline = runspace.CreatePipeline();
      Command scriptCommand = new Command(scriptFile);
      Collection&lt;CommandParameter&gt; commandParameters = new Collection&lt;CommandParameter&gt;();
      foreach (string scriptParameter in scriptParameters.Split(' '))
      {
          CommandParameter commandParm = new CommandParameter(null, scriptParameter);
          commandParameters.Add(commandParm);
          scriptCommand.Parameters.Add(commandParm);
      }
      pipeline.Commands.Add(scriptCommand);
      Collection&lt;PSObject&gt; psObjects;
      psObjects = pipeline.Invoke();
  }
  </code></pre>
  
  <p>ps:</p>
  
  <pre><code>Function Foo ($arg1, $arg2)
  {
      Write-Host $arg1
      Write-Host $arg2
  }
  Foo $args[0] $args[1]
  </code></pre>
  
  <p>What am i missing here? how can i make this work?</p>