powershellunicodecrashio-redirection

Looking for a workaround for `-RedirectStandardOutput` crash


I've discovered by accident that start-process crashes when a process started with the -RedirectStandardOutput outputs non-Latin characters.

Example python script:

print("Begin")
print("香港")
print("End")

PS command:

start-process python -argumentlist "debug.py" -RedirectStandardOutput debug.txt

The debug.txt contains only Begin line and nothing else. Python script by itself works fine. Obviously it is a UTF-8 encoding issue, and there are ways to mitigate this.

I am looking for a higher-level workaround which does not involve modifying the existing PS code, but rather wrapping it in another "driver" script.

System info:

python 3.12.8

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.26100.2161
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.26100.2161
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Solution

  • The simplest working solution was pointed out by @iRon and it is to set the Region > Administrative > Language for non-Unicode programs > Change system locale > Beta: Use Unicode UTF-8 for worldwide language support as described in https://stackoverflow.com/a/57134096/1701026.

    UPDATE:

    It was rightfully pointed out that this solution might cause many unwanted side-effects, so it has to be evaluated carefully for every case.