title: Redirect output to a TextBox in PowerShell using XAML
body: <p>I'm building a PowerShell script with a GUI using XAML. I'm calling a function (form a DLL file) that it's writing its output in the console, but I want to make it write in the TextBox instead. See image below to see what happens when I click on the button <code>CHECK DATA</code> which is calling the dummy method <code>set_Value</code> I've created to test.</p>
  
  <p><a href="https://i.stack.imgur.com/g41Az.png" rel="nofollow noreferrer">PS1-GUI Picture</a></p>
  
  <blockquote>
    <p>I've tried to use <a href="https://www.windows10forums.com/articles/windows-powershell-tutorial-5-advanced-redirection.69/" rel="nofollow noreferrer">redirections</a> with no success. I was only able to make it write to a file. I'm sure I'm missing something, but not sure what it is.</p>
  </blockquote>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="DataBase Export Import Tool" Height="469.28" Width="760" WindowStartupLocation="CenterScreen" ResizeMode='NoResize'&gt;
      &lt;Grid Margin="0,0,-0.2,0.2"&gt;
          &lt;Label Content="From Client" HorizontalAlignment="Left" Margin="19,10,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Content="To Client" HorizontalAlignment="Left" Margin="390,10,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Content="From Environment" HorizontalAlignment="Left" Margin="19,45,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Content="To Environment" HorizontalAlignment="Left" Margin="390,45,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Content="SQL Server" HorizontalAlignment="Left" Margin="19,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Name="prdSqlResult" Content="Server" HorizontalAlignment="Left" Margin="194,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/&gt;
          &lt;Label Content="SQL Server" HorizontalAlignment="Left" Margin="390,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Name="sqlResult" Content="Server" HorizontalAlignment="Left" Margin="565,80,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/&gt;
          &lt;Label Content="Database" HorizontalAlignment="Left" Margin="19,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Name="prdDbResult" Content="Database" HorizontalAlignment="Left" Margin="194,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/&gt;
          &lt;Label Content="Database" HorizontalAlignment="Left" Margin="390,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="#006271" Foreground="White"/&gt;
          &lt;Label Name="dbResult" Content="Database" HorizontalAlignment="Left" Margin="565,115,0,0" VerticalAlignment="Top" Height="30" Width="170" Background="White" Foreground="#006271"/&gt;
  
          &lt;TextBox Name="OutputBox" Height="200" Width="720" TextWrapping="Wrap" AcceptsReturn="True" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Margin="0,100,0,0" /&gt;
  
          &lt;Button Name="checkData" Content="Check Data" HorizontalAlignment="Left" Margin="19,387,0,0" Width="220" BorderThickness="0" RenderTransformOrigin="0.496,-2.677" Height="30" VerticalAlignment="Top"/&gt;
          &lt;Button Name="btndbe" Content="Run Database Export" HorizontalAlignment="Left" Margin="268,387,0,0" Width="220" BorderThickness="0" RenderTransformOrigin="0.496,-2.677" Height="30" VerticalAlignment="Top"/&gt;
          &lt;Button Name="btndbi" Content="Run Database Import" HorizontalAlignment="Left" Margin="516,387,0,0" Width="220" BorderThickness="0" RenderTransformOrigin="0.496,-2.677" Height="30" VerticalAlignment="Top"/&gt;
          &lt;StackPanel Margin="0"&gt;
          &lt;ComboBox Name="comboFromClient" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="194,10,0,0" Width="170"&gt;
              &lt;ComboBoxItem&gt;1&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;2&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;3&lt;/ComboBoxItem&gt;
          &lt;/ComboBox&gt;
      &lt;/StackPanel&gt;
      &lt;StackPanel Margin="0"&gt;
          &lt;ComboBox Name="comboFromEnvironment" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="194,45,0,0" Width="170"&gt;
              &lt;ComboBoxItem ToolTip="This it tool tip for item 1"&gt;PRD&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;A&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;B&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;C&lt;/ComboBoxItem&gt;
          &lt;/ComboBox&gt;
      &lt;/StackPanel&gt;
      &lt;StackPanel Margin="0"&gt;
          &lt;ComboBox Name="comboToClient" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="565,10,0,0" Width="170"&gt;
              &lt;ComboBoxItem&gt;1&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;2&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;3&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;CN&lt;/ComboBoxItem&gt;
          &lt;/ComboBox&gt;
      &lt;/StackPanel&gt;
      &lt;StackPanel Margin="0"&gt;
          &lt;ComboBox Name="comboToEnvironment" IsEditable="False" HorizontalAlignment="Left" Height="25" Margin="565,45,0,0" Width="170"&gt;
              &lt;ComboBoxItem&gt;A&lt;/ComboBoxItem&gt;
              &lt;ComboBoxItem&gt;B&lt;/ComboBoxItem&gt;
          &lt;/ComboBox&gt;
      &lt;/StackPanel&gt;        
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>PS1</strong></p>
  
  <pre><code>[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
  [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
  Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
  $XAML = [xml](Get-Content -Path "$PSSCriptRoot\GuiDataImportExport.xaml")
  $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
  
  #Dummy function to be able to get some output instead using the real method
  function set-Value{    
      for($i = 0; $i -lt 5; $i++){        
          $sortie = $i | timestamp
          Write-Host $sortie
          Sleep -Seconds 1
      }
  }
  
  function timestamp { Process{"$(Get-Date): $_"} }
  
  $Form=[Windows.Markup.XamlReader]::Load( $reader )
  $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
  
  $OutputBox = $Form.FindName("OutputBox")
  
  $checkData.Add_Click({
     #this writes in the console
     set-Value
  
     #This writes in a file
     $OutputBox.AppendText((&amp;{set-Value} *&gt;&gt; "$PSScriptRoot\log.log"))
  })
  
  $Form.ShowDialog() | out-null
  
  </code></pre>
  
  <p>Please let me know if you guys need more details.</p>