title: Add an existing Progressbar into an GUI
body: <p>I have writen an simple GUI for my command to display. When i press a button my script searches for matches in a Log File and while doing that it displays a Progressbar, but it only displays it in the ISE Window not in the GUI itself. How can i Display it in the GUI.</p>
  <p>I found New-Object System.Windows.Forms.ProgressBar when searching for a way.
  But in the examples i only found how they do new Bars not add existings that only exist inside of a Button.</p>
  <p>This is my Script</p>
  <pre><code>[void] [System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Drawing&quot;) 
  [void] [System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Windows.Forms&quot;) 
  
  #Dropdown/Serverauswahl
  Add-Type -AssemblyName System.Windows.Forms
  Add-Type -AssemblyName System.Drawing
  
  $form = New-Object System.Windows.Forms.Form
  $form.Text = 'Select a Computer'
  $form.Size = New-Object System.Drawing.Size(600,400)
  $form.StartPosition = 'CenterScreen'
  
  $okButton = New-Object System.Windows.Forms.Button
  $okButton.Location = New-Object System.Drawing.Point(150,240)
  $okButton.Size = New-Object System.Drawing.Size(150,46)
  $okButton.Text = 'OK'
  $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  $form.AcceptButton = $okButton
  $form.Controls.Add($okButton)
  
  $cancelButton = New-Object System.Windows.Forms.Button
  $cancelButton.Location = New-Object System.Drawing.Point(300,240)
  $cancelButton.Size = New-Object System.Drawing.Size(150,46)
  $cancelButton.Text = 'Cancel'
  $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  $form.CancelButton = $cancelButton
  $form.Controls.Add($cancelButton)
  
  $label = New-Object System.Windows.Forms.Label
  $label.Location = New-Object System.Drawing.Point(10,20)
  $label.Size = New-Object System.Drawing.Size(560,40)
  $label.Text = 'Please select a Server:'
  $form.Controls.Add($label)
  
  $listBox = New-Object System.Windows.Forms.ListBox
  $listBox.Location = New-Object System.Drawing.Point(20,80)
  $listBox.Size = New-Object System.Drawing.Size(540,40)
  $listBox.Font = &quot;courier New, 13&quot;
  $listBox.Height =150
  
  [void] $listBox.Items.Add('LNS5')
  [void] $listBox.Items.Add('LNS10')
  [void] $listBox.Items.Add('LNS13')
  [void] $listBox.Items.Add('LNS14')
  [void] $listBox.Items.Add('LNS62')
  
  $form.Controls.Add($listBox)
  
  $form.Topmost = $true
  
  $result = $form.ShowDialog()
  
  if ($result -eq [System.Windows.Forms.DialogResult]::OK)
  {
      $x = $listBox.SelectedItem
      $path = &quot;C:\temp\SMTPFilter\${x}filter.txt&quot;
  }
  
  #zweites Fenster   
  $objForm = New-Object System.Windows.Forms.Form
  $objForm.StartPosition = &quot;CenterScreen&quot;
  $objForm.Size = New-Object System.Drawing.Size(1200,800)
  $objForm.Text = &quot;Test GUI&quot;
  
  $form = New-Object System.Windows.Forms.Form
  $form.Size = New-Object System.Drawing.Size(900,600)
  $dataGridView = New-Object System.Windows.Forms.DataGridView
  $dataGridView.Size=New-Object System.Drawing.Size(800,400)
  
  #Filtern
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Size(30,112)
  $OKButton.Size = New-Object System.Drawing.Size(300,92)
  $OKButton.Text = &quot;Filtern&quot;
  $OKButton.Name = &quot;Filter&quot;
  $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::None
  $OKButton.Add_Click({$i= 0
  $length = (Get-Content $path).Length
  
  $global:result = Get-Content $path | ForEach-Object {
      if($_ -match '(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}).*\(((?:\d{1,3}\.){3}\d{1,3})\) disconnected\.?\s+(\d+) message\[s\]'){
          [PsCustomObject]@{
              IP       = $matches[2]
              Messages = [int]$matches[3]
              Date     = [datetime]::ParseExact($matches[1], 'dd.MM.yyyy HH:mm:ss', $null)
          }}
  
          $i++
      if($i % 1000 -eq 0){
          Write-Progress -activity &quot;Searching for matches&quot; -status &quot;Scanned: $i of $($length)&quot; -percentComplete (($i / $length)  * 100)
      
   }}
  
   Write-Progress -activity &quot;Searching for matches&quot; -status &quot;Scanned: $i of $($length)&quot; -percentComplete (($i / $length)  * 100)
  
   #Messages Counted
   $global:cumulative = $result | Group-Object -Property IP | ForEach-Object {
  
      try {
          $dns = [System.Net.Dns]::GetHostEntry($_.Name).HostName
      }
      catch { 
          $dns = 'Not available' 
      }
  
      [PsCustomObject]@{
          IP = $_.Name
          Messages = ($_.Group | Measure-Object -Property Messages -Sum).Sum
          DNSName = $dns
          Date    = ($_.Group | Sort-Object Date)[-1].Date
      }
   }})
  $objForm.Controls.Add($OKButton)
  
  #Ergebnis Anzeigen
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Size(30,214)
  $OKButton.Size = New-Object System.Drawing.Size(300,92)
  $OKButton.Text = &quot;Ergebnis anzeigen&quot;
  $OKButton.Name = &quot;Egebnis Button&quot;
  $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::None
  $OKButton.Add_Click({$objTextBox1 = New-Object System.Windows.Forms.TextBox 
  $objTextBox1.Multiline = $True;
  $objTextBox1.Location = New-Object System.Drawing.Size(360,10) 
  $objTextBox1.Size = New-Object System.Drawing.Size(800,600)
  $objTextBox1.Text = $cumulative | Out-String
  $objTextBox1.Font = &quot;courier New, 13&quot;
  $objTextBox1.Scrollbars = &quot;Vertical&quot;
  $objForm.Controls.Add($objTextBox1)
  #outgridview
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Size(30,316)
  $OKButton.Size = New-Object System.Drawing.Size(300,92)
  $OKButton.Text = &quot;Ergebnis in GridView&quot;
  $OKButton.Name = &quot;GridView&quot;
  $OKButton.DialogResult = &quot;OK&quot;
  $OKButton.Add_Click({$cumulative | Out-GridView})
  $objForm.Controls.Add($OKButton)
  #Export CSV
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Size(30,418)
  $OKButton.Size = New-Object System.Drawing.Size(300,92)
  $OKButton.Text = &quot;Export CSV (in C:/temp)&quot;
  $OKButton.Name = &quot;CSV&quot;
  $OKButton.DialogResult = &quot;OK&quot;
  $OKButton.Add_Click({$cumulative | Export-Csv -Path 'C:\temp\SMTPresult.Csv'})
  $objForm.Controls.Add($OKButton) })
  $objForm.Controls.Add($OKButton) 
  
       [void] $objForm.ShowDialog()
  </code></pre>