title: Executing powershell scripts in c#
body: <p>Below is the script I am using to try and execute my powershell script but whenever i run it i just get a blank command window.</p>
  
  <h3>C# Code</h3>
  
  <pre><code>static void Main(string[] args)
  {
      string text = System.IO.File.ReadAllText(@"C:\Program Files (x86)\Backup Reporter\Required\edit_website.ps1");
  
      using (PowerShell PowerShellInstance = PowerShell.Create())
      {
          // use "AddScript" to add the contents of a script file to the end of the execution pipeline.
          // use "AddCommand" to add individual commands/cmdlets to the end of the execution pipeline.
          PowerShellInstance.AddScript(text);
  
          Collection&lt;PSObject&gt; PSOutput = PowerShellInstance.Invoke();
          foreach (PSObject outputItem in PSOutput)
          {
              // if null object was dumped to the pipeline during the script then a null
              // object may be present here. check for null to prevent potential NRE.
              if (outputItem != null)
              {
                  Console.WriteLine(outputItem.BaseObject.ToString() + "\n");
              }
          }
          if (PowerShellInstance.Streams.Error.Count &gt; 0)
          {
              Console.Write("Error");
          }
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <h3>Powershell Script</h3>
  
  <pre><code> $text = "test test test"
  </code></pre>
  
  <p>All I want to do is output test to the command window.</p>