Encoding
It turns out that files are created with different
encoding when using
>
in PowerShell 5.1 and PowerShell 7.1.
This is demonstrated with the following test script. It creates a file abc.txt
and then displays each byte in the file that was created:
'abc' > abc.txt
if ($psVersionTable.psEdition -eq 'Desktop') {
$bytes = get-content abc.txt -encoding byte
}
else {
$bytes = get-content abc.txt -asByteStream
}
foreach ($byte in $bytes) {
' {0:x2} {1}' -f $byte, [char]$byte
}
ff ÿ
fe þ
61 a
00
62 b
00
63 c
00
0d
00
0a
00
PowerShell Core is more reasonable and does not create the file with a BOM and uses
UTF-8(?)
61 a
62 b
63 c
0d
0a
Because the redirection operators are aliased to
out-file
, the encoding for the files that are produced with the redirection operators can be controlled with the
$psDefaultParameterValues
variable:
$psDefaultParameterValues['out-file:encoding'] = 'utf8'