The output from stdout in the remote session is translated to UTF-8 as noted by Jordan Borean in https://github.com/PowerShell/PowerShell/issues/15128:
This is all happening on the remote side, anything exchanged across PSSessions are in a utf-8 encoded CLIXML payload so there's no chance of an encoding mismatch between the client and the server. We just need to make sure that the remote PowerShell session captures the stdout from the process properly before it is serialised as a string to the CLIXML.
The output from stdout in the remote session is coded using an unspecified multi bytes code page. Mojibake is observed when these strings are displayed "as is" across the Powershell session.
When this output (sequence of characters) is properly encoded into a sequence of bytes, we recover the [System.Text.Encoding]::Default encoded value as an intermediary step.
Decoding this data using the system locale $(Get-WinSystemLocale).TextInfo.OEMCodePage restores the intended output from the native utility. To be clear, you get the UTF-8 representation of the intended output. This is the goal.
In a remote PowerShell session, we have no idea of the character sets in use. Basically, you would need to review in your script any and all output from a native utility:
- your native output = $(stdout from a native utility command line)
- $Encoding = [System.Text.Encoding]::Default
- $SourceEncoding = $(Get-WinSystemLocale).TextInfo.OEMCodePage
- $Output = [System.Text.Encoding]::GetEncoding([int]$SourceEncoding)
- $Output.GetString( $Encoding.GetBytes( your native output ) )
All of this is happening transparently on the local console and you may not be aware of the gibberish displayed from a random remote system until you see it.
Note the call to Get-WinSystemLocale: the OEM codepage is set on the remote system. Connecting the test script below to different servers yields the correct result regardless of this issue. Shown below are results from two distinct servers using codepages 850 and 437.
Now, why this has to be done for remote session as opposed to local session is still a mystery to me.
Enjoy!
Windows Server 2025 Standard 24H2 (Build 26100.33650)
This is PowerShell 5.1.26100.33648
Remote Console Output Encoding is READ ONLY: True
Remote Console Output Encoding is System encoding : True
Testing if captured characters using iso-8859-1 are restored to UTF-8 values through ibm850:
Test codepoint for à: True
Test codepoint for â: True
Test codepoint for è: True
Test codepoint for ê: True
Test codepoint for é: True
Test codepoint for ô: True
Test codepoint for ù: True
Windows Server 2022 Standard 21H2 (Build 20348.5761)
This is PowerShell 5.1.20348.5756
Remote Console Output Encoding is READ ONLY: True
Remote Console Output Encoding is System encoding : True
Testing if captured characters using iso-8859-1 are restored to UTF-8 values through IBM437:
Test codepoint for à: True
Test codepoint for â: True
Test codepoint for è: True
Test codepoint for ê: True
Test codepoint for é: True
Test codepoint for ô: True
Test codepoint for ù: True
Test script: (Replace localhost by any computer able to do PSRemoting ;-)
Invoke-Command -ComputerName localhost -ScriptBlock {
### Display OS and PowerShell versions
$os = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
Write-Output ""
Write-Output "$($os.ProductName) $($os.DisplayVersion) (Build $($os.CurrentBuildNumber).$($os.UBR))"
Write-Output "This is PowerShell $($PSVersionTable.PSVersion -join ".")"
Write-Output ""
Write-Output "Remote Console Output Encoding is READ ONLY: $([Console]::OutputEncoding.IsReadOnly)"
Write-Output "Remote Console Output Encoding is System encoding : $([Console]::OutputEncoding -eq [System.Text.Encoding]::Default)"
$Encoding = [System.Text.Encoding]::Default
Write-Output ""
### Presume legacy code page
$SourceEncoding = $(Get-WinSystemLocale).TextInfo.OEMCodePage
$Output = [System.Text.Encoding]::GetEncoding([int]$SourceEncoding)
### List sparse codepoints
$CodePoint = @()
$CodePoint += @("à", 8230)
$CodePoint += @("â", 402)
$CodePoint += @("è", 352)
$CodePoint += @("ê", 710)
$CodePoint += @("é", 8218)
$CodePoint += @("ô", 8220)
$CodePoint += @("ù", 8212)
### Test text restore
Write-Output "Testing if captured characters using $([System.Text.Encoding]::Default.BodyName) are restored to UTF-8 values through $($Output.BodyName):"
For ($i = 0; $i -lt $CodePoint.Count; $i = $i + 2) {
Write-Output "Test codepoint for $($CodePoint[$i]): $($CodePoint[$i] -eq $Output.GetString( $Encoding.GetBytes( @([char] $CodePoint[$i + 1] ) ) ))"
}
Write-Output ""
}