Search code examples
powershellstreamwriter

StreamWriter(Stream, Encoding) not working in PowerShell


I wanted to convert a file from UTF-16 to ANSI with Powershell and I encountered some behavior that I don't understand.

My (simplified) script is

$encoding = [System.Text.Encoding]::GetEncoding(1250)

$sr = New-Object System.IO.StreamReader -Arg c:\utf16.txt
$sw = New-Object System.IO.StreamWriter c:\new.txt, $false, $encoding

 while ($line = $sr.ReadLine()) {
    $sw.WriteLine($line)
 }

$sr.close()
$sw.Close()

And this is working fine, the output new.txt is in ANSI encoding However, if I change

$sw = New-Object System.IO.StreamWriter c:\new.txt, $false, $encoding

to

$sw = New-Object System.IO.StreamWriter c:\new.txt, $encoding

the output new.txt is in UTF-8 encoding. According to the StreamWriter Class documentation, this is also a valid StreamWriter Constructor to define the encoding.

What am I missing ?

Tom


Solution

  • You're missing that C:\new.txt still is a string, not a Stream object. Meaning that you're invoking the StreamWriter(String, Boolean) constructor, not the StreamWriter(Stream, Encoding) constructor. $encoding is implicitly converted to boolean in this context. The former constructor creates a UTF-8 writer as documented:

    Remarks

    This constructor creates a StreamWriter with UTF-8 encoding without a Byte-Order Mark [...]