Get-Content might be not optimal as it handles the input file line by line (at least, by default, if you don't use the Raw
switch as described later), and may cause changing the line ending (for example, if you move text files between Unix and Windows systems). I had serious problems in a script just because that, and it took about an hour to find the exact reason. See more about that in this post. Due to this behavior, Get-Content is not the best choice as well, if performance matters.
Instead of this, you can use PowerShell in combination of the .NET classes (as long you have a version of the .NET Framework installed on your system):
$sr = New-Object System.IO.StreamReader($infile)
$sw = New-Object System.IO.StreamWriter($outfile, $false, [System.Text.Encoding]::Default)
$sw.Write($sr.ReadToEnd())
$sw.Close()
$sr.Close()
$sw.Dispose()
$sr.Dispose()
Or even more simply, use the Raw
switch as described here to avoid that overhead and read the text in a single block:
Get-Content $inFile -Raw
You've initialized your StreamsReader and StreamWriter with wrong encoding. – None – 2018-03-07T15:46:23.693