title: Read UTF-8 files correctly with PowerShell
body: <p>Following situation:</p>
  
  <ul>
  <li>A PowerShell script creates a file with UTF-8 encoding</li>
  <li>The user may or may not edit the file, possibly losing the BOM, but should keep the encoding as UTF-8, and possibly changing the line separators</li>
  <li>The same PowerShell script reads the file, adds some more content and writes it all as UTF-8 back to the same file</li>
  <li>This can be iterated many times</li>
  </ul>
  
  <p>With <code>Get-Content</code> and <code>Out-File -Encoding UTF8</code> I have problems reading it correctly. It's stumbling over the BOM it has written before (putting it in the content, breaking my parsing regex), does not use UTF-8 encoding and even deletes line breaks in the original content part.</p>
  
  <p>I need a function that can read any file with UTF-8 encoding, ignore and delete the BOM and not modify the content. What should I use?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I have added a little test script that shows what I'm trying to do and what happens instead.</p>
  
  <pre><code># Read data if exists
  $data = ""
  $startRev = 1;
  if (Test-Path test.txt)
  {
      $data = Get-Content -Path test.txt
      if ($data -match "^[0-9-]{10} - r([0-9]+)")
      {
          $startRev = [int]$matches[1] + 1
      }
  }
  Write-Host Next revision is $startRev
  
  # Define example data to add
  $startRev = $startRev + 10
  $newMsgs = "2014-04-01 - r" + $startRev + "`r`n`r`n" + `
      "Line 1`r`n" + `
      "Line 2`r`n`r`n"
  
  # Write new data back
  $data = $newMsgs + $data
  $data | Out-File test.txt -Encoding UTF8
  </code></pre>
  
  <p>After running it a few times, new sections should be added to the beginning of the file, the existing content should not be altered in any way (currently loses line breaks) and no additional new lines should be added at the end of the file (seems to happen sometimes).</p>
  
  <p>Instead, the second run gives me an error.</p>