title: How to read PowerShell exit code via c#
body: <p>I am using <code>System.Management.Automation.Runspaces</code> to execute PowerShell scripts.
  is there an option i can read the exit code of a given script?</p>
  
  <pre><code>using System.IO;
  using System.Management.Automation.Runspaces;
  using System.Collections.ObjectModel;
  using System.Management.Automation;
  
  namespace PowerShell
  {
      public class PowerShellExecuter
      {
          public Collection&lt;PSObject&gt; RunPsScript(string psScriptFile)
          {
              string psScript;
              if (File.Exists(psScriptFile))
              {
                  psScript = File.ReadAllText(psScriptFile);
              }
              else
              {
                  throw new FileNotFoundException("Wrong path for the script file");
              }
              Runspace runSpace = RunspaceFactory.CreateRunspace();
              runSpace.Open();
  
              RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
              runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
  
              Pipeline pipeLine = runSpace.CreatePipeline();
              pipeLine.Commands.AddScript(psScript);
              pipeLine.Commands.Add("Out-String");
  
              Collection&lt;PSObject&gt; returnObjects = pipeLine.Invoke();
              runSpace.Close();
  
              return returnObjects;
          }
      }
  }
  </code></pre>