title: Making an Powershell Progress Bar more efficient
body: <p>I build a script to filter several Messages out of a log file. The file im using right now has around 400.000 lines and when im looking for a match with the following code he takes very long since i added the progress bar. Is there a way to make it more efficient. If im right the reason for it to take so long is that he refreshes the progressbar Gui with every line he passes.</p>
  <pre><code>$i= 0
  $path = &quot;&quot;
  $length = (Get-Content $path).Length
  
  #Datum, Hostname und Message Nummer
  $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\]'){
          try {
              $dns = [System.Net.Dns]::GetHostEntry($matches[2]).HostName
          }
          catch { 
              $dns = 'Not available' 
          }
          [PsCustomObject]@{
              IP       = $matches[2]
              Messages = [int]$matches[3]
              DNSName  = $dns
              Date     = [datetime]::ParseExact($matches[1], 'dd.MM.yyyy HH:mm:ss', $null)
          }
      }
       # update counter and write progress
     $i++
     Write-Progress -activity &quot;Searching for matches&quot; -status &quot;Scanned: $i of $($length)&quot; -percentComplete (($i / $length)  * 100)
   }
  
   #Messages Counted
   $cumulative = $result | Group-Object -Property IP | ForEach-Object {
      [PsCustomObject]@{
          IP = $_.Name
          Messages = ($_.Group | Measure-Object -Property Messages -Sum).Sum
          DNSName = $_.Group[0].DNSName
          Date    = ($_.Group | Sort-Object Date)[-1].Date
      }
  }
  </code></pre>