title: Is there a way to increase write speed to file for this script
body: <p>is there a way to speed up and optimize this .ps1 script. It writes to file at around 1KB/s which is kind of slow if files are bigger than few MB.</p>
  
  <p>This script reads file by line and increments each occurence of \&lt;x but each line number resets.</p>
  
  <p>It's my first script so that's why its so barbaric.</p>
  
  <pre><code>$reader = [System.IO.File]::OpenText("file")
  try {
  for() {
      $line = $reader.ReadLine()
      if ($line -eq $null) { break }
  
      $numbers =  [System.Text.RegularExpressions.Regex]::Matches($line, "\&amp;lt;x")
  
      for ($i = $numbers.Count - 1; $i -ge 0; $i--)
      {
  
          Write-Host ($numbers[$i].Index + $numbers[$i].Length)
          $line = $line.Substring(0, $numbers[$i].Index) +  "\&amp;lt;x" + ($i + 1).ToString() +  $line.Substring($numbers[$i].Index + $numbers[$i].Length)
      } 
  
      $line | Out-File "outfile" -Append
  }   
  }
  finally {
  $reader.Close() 
  } 
  </code></pre>