Search notes:

PowerShell: Encoding

Determine a PowerShell's session encoding

$defaultEncoding = `
[psobject].Assembly.GetType('System.Management.Automation.ClrFacade').                    `
     GetMethod('GetDefaultEncoding', [System.Reflection.BindingFlags]'nonpublic,static'). `
     Invoke($null, @())

$defaultEncoding.BodyName
$defaultEncoding.CodePage

$defaultEncoding.GetType().FullName

Encodings of Scripts

In order to gain some clarity on the required encoding of PowerShell scripts, I created three scripts in different encodings that write ïthe length of the string 'Bär'.
The three encodings with which the scripts are encoded are
For example, the script encoded in UTF-8 without a bom is:
"PowerShell version: $($psVersionTable.psVersion)"
"This file is encoded in utf-8 without a bom"
'Bär'.length
Github repository about-PowerShell, path: /encoding/script-utf8.ps1
I used these scripts in PowerShell 7.1 and PowerShell 5.1. The following is a summary of the results.

Invocation in PowerShell 7.1

In PowerShell 7.1, the length of 'Bär' is always determined to be 3:
PS> .\script-latin1.ps1
PowerShell version: 7.1.0-preview.6
This file is encoded in latin1
3
PS> .\script-utf8.ps1
PowerShell version: 7.1.0-preview.6
This file is encoded in utf-8 without a bom
3
PS> .\script-utf8-bom.ps1
PowerShell version: 7.1.0-preview.6
This file is encoded in utf-8 with a bom
3
As per Michael Klement's answer on Stackoverflow to a related question of mine, the reason that length is reported as 3, even though the script is encoded in Latin-1, is because the ä is encoded as Unicode U+FFFD (which is used to indicate a character whose value is unknown in Unicode).

PowerShell 5.1

In PowerShell 5.1 however, the length differs between latin-1 and UTF-8 if a script BOM is indicated:
PS> .\script-latin1.ps1
PowerShell version: 5.1.19041.610
This file is encoded in latin1
3
PS> .\script-utf8.ps1
PowerShell version: 5.1.19041.610
This file is encoded in utf-8 without a bom
4
PS> .\script-utf8-bom.ps1
PowerShell version: 5.1.19041.610
This file is encoded in utf-8 with a bom
3

See also

Cmdlets that have an -encoding parameter.
The preference variable $outputEncoding

Index