title: StreamWriter encoding
body: <p>I am processing some very large files to find and replace strings, so I need to use StreamReader and StreamWriter. And I also need to support multiple encoding. I have </p>
  
  <pre><code>$reader = [IO.StreamReader]::New("\\Mac\Support\Journal Tools\Aaron\ANSI.txt")
  $writer = [IO.StreamWriter]::New("\\Mac\Support\Journal Tools\Aaron\stream.txt", $reader.CurrentEncoding)
  </code></pre>
  
  <p>It doesn't throw any kind of error, but no matter what the source encoding the output file is always 'System.Text.UTF8Encoding'. Obviously I am missing something basic, but the lack of a thrown error has me stumped.</p>
  
  <p>EDIT: I tried forcing ASCII with the constructor above, like this</p>
  
  <pre><code>$writer = [IO.StreamWriter]::New("\\Mac\Support\Journal Tools\Aaron\stream.txt", [System.Text.ASCIIEncoding])
  </code></pre>
  
  <p>But still somehow the output is UTF8, but with no errors. Curriouser and curiouser.</p>
  
  <p>EDIT 2: So, based on comments, I tried forcing ASCII, and I added here the code I am using to see what the results file is encoded as. Maybe THAT'S where I have gone wrong?</p>
  
  <pre><code>$reader = [IO.StreamReader]::New("\\Mac\Support\Journal Tools\Aaron\ANSI.txt")
  $writer = [IO.StreamWriter]::New("\\Mac\Support\Journal Tools\Aaron\stream.txt", [System.Text.Encoding]::ASCII)
  
  try {
      while (-not ($reader.EndOfStream)) {
          $line = $reader.ReadLine()
          $writer.WriteLine($line)
      }
  }
  finally {
      $reader.Close(); $reader.Dispose()
      $writer.Close(); $writer.Dispose()
  }
  
  $reader.Close(); $reader.Dispose()
  $writer.Close(); $writer.Dispose()
  
  $test = [IO.StreamReader]::New("\\Mac\Support\Journal Tools\Aaron\stream.txt")
  Write-Host "$($test.CurrentEncoding)!!!"
  $test.Close(); $test.Dispose()
  </code></pre>
  
  <p>And ultimately I need to be able to convert the text expression that I get from .CurrentEncoding into the proper format for the Constructor. Is there some function available for that, or am I going to be making my own table for that conversion?</p>
  
  <p>Hmm, weird. I have a file that I can verify is a UNICODE file via NotePad++, but this still reports UTF8.</p>
  
  <pre><code>$reader = [IO.StreamReader]::New("\\Mac\Support\Journal Tools\Aaron\UNICODE.txt", $true)
  Write-Host "$($reader.CurrentEncoding)"
  $reader.Close(); $reader.Dispose()
  </code></pre>
  
  <p>And yet, this little function returns Unicode correctly. </p>
  
  <pre><code>function Get-PxFileEncoding {
      [CmdletBinding()]
      param ( 
          [parameter(Mandatory=$true)][String]$filePath
      )
  
      [Byte[]] $byte = get-content -path:$filePath -encoding:Byte -readCount:4 -totalCount:4
  
      if ($byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf) { 
          $encoding = 'UTF8' 
      } elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { 
          $encoding = 'BigEndianUnicode' 
      } elseif ($byte[0] -eq 0xff -and $byte[1] -eq 0xfe) { 
           $encoding = 'Unicode' 
      } elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { 
          $encoding = 'UTF32' 
      } elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { 
          $encoding = 'UTF7'
      } else { 
          $encoding = 'ASCII' 
      }
  
  $encoding
  }
  </code></pre>
  
  <p>Seems like there's a bug here perhaps. Can someone else just verify if that 3 liner above returns Unicode for you? FWIW, this is a Windows 7 VM upgraded to PS5.</p>