title: Powershell - Run external powershell script and capture output - Can't pass parameters
body: <p>I have a problem running a powershell script from within another powershell script, passing parameters and capturing the output. I have tried using the ampersand operator, calling it via <code>powershell.exe -Command</code> but nothing seems to work.<br></p>
  <p>What seems to work is using fixed parameter and values stored in a variable like this <code>C:\path\to\script.ps1 -arg1 $value</code>.<br>
  This may present a solution if nothing else works, but I would like to run the command similar to this <code>&amp; $pathToScript $params 2&gt;&amp;1</code> (the <code>2&gt;&amp;1</code>is for capturing error output as well as standard).</p>
  <hr />
  <p>Sometimes the construct prints just the path to the script, <br>
  sometimes it says <code>Cannot run file in the middle of pipeline</code> and <br>sometimes it complains about it cannot find the mentioned script file (I sometimes have spaces in my path, but I thought quoting it would suffice: quoting was done like this <code>$path = &quot;`&quot;C:\path\with space\to\script.ps1`&quot;&quot;</code>).</p>
  <p>This is the simplified function I want to use this in:</p>
  <pre><code>Function captureScriptOutput
  {
      #the function receives the script path and parameters
      param($path, $params)
      
      #this works if no params are passed, but I need params!
      $output = &amp; $path $params 2&gt;&amp;1 | Out-String
  }
  </code></pre>