title: Formatting Output of Hosted PowerShell
body: <p>I found a C# example <a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-Windows-PowerShell-Programming-Snapins-Cmdlets-Hosts-and-Providers.productCd-0470173939,descCd-DOWNLOAD.html" rel="noreferrer">here</a> of invoking a PowerShell script asynchronously from a host application (in folder Chapter 6 - Reading With Events) and am trying to use it in a Windows Forms application. </p>
  
  <p>I have a button (button1) to start the PowerShell script, textBox1 is to enter the script and textBox2 displays the script output. Here is my current code:</p>
  
  <pre><code>using System;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;
  using System.Windows.Forms;
  
  namespace PSTestApp
  {
  
      delegate void SetTextDelegate(string text);
  
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              textBox2.Text = "";
  
              Runspace runspace = 
                  RunspaceFactory.CreateRunspace();
              runspace.Open();
  
              Pipeline pipeline = 
                  runspace.CreatePipeline(textBox1.Text);
  
              pipeline.Output.DataReady += 
                  new EventHandler(HandleDataReady);
              pipeline.Error.DataReady += 
                  new EventHandler(HandleDataReady);
  
              pipeline.InvokeAsync();
              pipeline.Input.Close();
          }
  
          private void HandleDataReady(object sender, EventArgs e)
          {
              PipelineReader&lt;PSObject&gt; output = 
                  sender as PipelineReader&lt;PSObject&gt;;
  
              if (output != null)
              {
                  while (output.Count &gt; 0)
                  {
                      SetText(output.Read().ToString());
                  }
                  return;
              }
  
              PipelineReader&lt;object&gt; error = 
                  sender as PipelineReader&lt;object&gt;;
  
              if (error != null)
              {
                  while (error.Count &gt; 0)
                  {
                      SetText(error.Read().ToString());
                  }
                  return;
              }
          }
  
          private void SetText(string text)
          {
              if (textBox2.InvokeRequired)
              {
                  SetTextDelegate d = new SetTextDelegate(SetText);
                  this.Invoke(d, new Object[] { text });
              }
              else
              {
                  textBox2.Text += (text + Environment.NewLine);
              }
          }
      }
  }
  </code></pre>
  
  <p>The code works, but I have a problem handling the output. Pipeline.Output.Read() returns an instance of PSObject so ToString() returns different things for different objects. For example, if I use this PowerShell command:</p>
  
  <pre><code>Get-ChildItem
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>PSTestApp.exe
  PSTestApp.pdb
  PSTestApp.vshost.exe
  PSTestApp.vshost.exe.manifest
  </code></pre>
  
  <p>and if I use:</p>
  
  <pre><code>Get-Process
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>...
  System.Diagnostics.Process (csrss)
  System.Diagnostics.Process (ctfmon)
  System.Diagnostics.Process (devenv)
  System.Diagnostics.Process (devenv)
  ...
  </code></pre>
  
  <p>I could use the returned PSObject instances to construct the output, but it would be nice If I could use the existing PowerShell formatting and get the same output as in the console. When I run the application and check Runspace.RunspaceConfiguration.Formats, the count is 9, and DotNetTypes.format.ps1xml is present, but I don't know how to apply the format.</p>
  
  <p>I have noticed that if I add Out-String at the end of the script:</p>
  
  <pre><code>...
  Pipeline pipeline = 
      runspace.CreatePipeline(textBox1.Text);
  pipeline.Commands.Add("Out-String");
  ...
  </code></pre>
  
  <p>the output is formatted just as in the standard PowerShell console. This works, but if I run a script with a long output that takes some time to execute:</p>
  
  <pre><code>gci d:\ -recurse
  </code></pre>
  
  <p>Pipeline.Output.DataReady event is raised only once (after the ending Out-String is executed) and only then is the output added to the text box.</p>
  
  <p>Is there a way to use standard PowerShell output formatting in a hosted PowerShell instance?</p>