title: More efficient way to write to an output file in Powershell needed
body: <p>I have a Powershell script i'm using to parse each row in a file, reformat it, and write the new string to an output file. It works fine with an input file with a few hundred lines. However, I need to ultimately run it against a file with a few million lines, and I have been waiting hours and it still hasn't finished. Following <a href="https://stackoverflow.com/questions/8755497/which-should-i-use-write-host-write-output-or-consolewriteline">this post</a>, I think I need to put Write-Output outside of the loop, but i've been unsuccessful so far.</p>
  
  <p>This is my current code:</p>
  
  <pre><code>Foreach ($line in Get-Content $logFile) {
  
      $arr = $line.Split()
  
      $port1 = $arr[9].Split(":")
  
      $port2 = $arr[11].Split(":")
  
      $connstring = '|' + $port1[0] + "|" + $port1[1] + "|" + $port2[0] + "|" + $port2[1] + "|" + $arr[4] + "|"
  
      Write-Output $connstring | Out-File "C:\logging\output\logout.txt" -Append 
  }
  </code></pre>
  
  <p>An example of an input string is:</p>
  
  <pre><code>06/14-04:40:11.371923  [**] [1:4:0] other [**] [Priority: 0] {TCP} 67.202.196.92:80 -&gt; 192.168.1.105:55043
  </code></pre>
  
  <p>And I need to reformat it to this:</p>
  
  <pre><code>|67.202.196.92|80|192.168.1.105|55043|other|
  </code></pre>
  
  <p>Any help is much appreciated!</p>