title: How do I debug a powershell script executed from C#?
body: <p>I have code running a powershell Script though the use of the <code>System.Management.Automation</code> namespace in C#, similar to the code below.</p>
  
  <pre><code> using (mPowershell = PowerShell.Create()) 
   {
          mPowershell.AddScript(GetScriptText(mStep), true);
          SetPowershellVariables(mPowershell);
          output = new PSDataCollection&lt;PSObject&gt;();
          output.DataAdded += new EventHandler&lt;DataAddedEventArgs&gt;(Output_DataAdded);
          mPowershell.InvocationStateChanged += new EventHandler&lt;PSInvocationStateChangedEventArgs&gt;(Powershell_InvocationStateChanged);
          IAsyncResult asyncResult = mPowershell.BeginInvoke&lt;PSObject, PSObject&gt;(null, output);      
  }
  </code></pre>
  
  <p>As expected I am getting errors, and I would like to debug them. </p>
  
  <p>Is there a way, without going into the powershell script and putting a <code>Write-Host $somevariable</code>  every 2 lines, to step by step debug this script?</p>
  
  <p>I should mention the script itself cannot run standalone, the C# code adds variables to the runspace of the script.</p>