title: Sending live output from Exchange powershell to textbox in C#
body: <p>I have an Exchange Powershell script that will move email from one account to another. when you invoke the code thru Powershell it provides you with real time data stats. how can I capture that and feed it into a text box that will provide that "live feed". Thanks in advance for any assistance.</p>
  
  <pre><code>    public void RunPowerShell()
      {            
          Cursor.Current = Cursors.WaitCursor;
          RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
          PSSnapInException snapInException = null;
          Runspace runSpace;
  
          //create the runspace
          runSpace = RunspaceFactory.CreateRunspace(rsConfig);
  
          //insert try here
          runSpace.Open();
  
          //for exchange 2010 use "Microsoft.Exchange.Management.PowerShell.E2010"
          rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
  
          //set up the pipeline to run the powershell command 
          Pipeline pipeLine = runSpace.CreatePipeline();
  
          //create the scripts to use
          String sScript = "Add-MailboxPermission -Identity " + FromEmailAccount.Text + " -User Administrator -AccessRights FullAccess";
          String sScript1 = "Add-MailboxPermission -Identity " + ToEmailAccount.Text + " -User Administrator -AccessRights FullAccess";
          String sScript2 = "Export-Mailbox -Identity " + FromEmailAccount.Text + " -TargetMailbox " + ToEmailAccount.Text + " -TargetFolder " + FromEmailAccount.Text + " -Confirm:$false";
  
          //invoke the scripts
          pipeLine.Commands.AddScript(sScript);
          pipeLine.Commands.AddScript(sScript1);
          pipeLine.Commands.AddScript(sScript2);
  
          Collection&lt;PSObject&gt; commandResults = pipeLine.Invoke();
  
          //loop through the results of the command and load the ?
          foreach (PSObject results in commandResults)
          {
              richTextBox1.Text = results.ToString();
              //MessageBox.Show(results.Properties["DisplayName"].Value.ToString(),@"Name");
          }
  
          //close the pipelin and runspace
          pipeLine.Dispose();
          runSpace.Close();
  
          //create completed message
          MessageBox.Show(@"Email migration is complete", @"Done");
          Cursor.Current = Cursors.Default;
      }
  </code></pre>