How to output file in the right encoding by PowerShell?

226 Views Asked by At

I'm trying to output my log file to disk in PowerShell, but i get wrong file content.

Info as follow:

Windows 10 Enterprise LTSC 21H2

PowerShell Version:

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Default Encoding:

Preamble          :
BodyName          : utf-8
EncodingName      : Unicode (UTF-8)
HeaderName        : utf-8
WebName           : utf-8
WindowsCodePage   : 1200
IsBrowserDisplay  : True
IsBrowserSave     : True
IsMailNewsDisplay : True
IsMailNewsSave    : True
IsSingleByte      : False
EncoderFallback   : System.Text.EncoderReplacementFallback
DecoderFallback   : System.Text.DecoderReplacementFallback
IsReadOnly        : True
CodePage          : 65001

Scene:

adb logcat WeatherActivity:D *:S > weather.log

the log file outputed in powershell

When i do the same thing in git bash or mobaxterm, it get the right content like this.

the log file outputed in git bash

What i have tried: Change PowerShell version from 5.1 to 7.3.6

The PowerShell 7 use UTF-8 to out-file by default, so it may not be this reason?

Releated work i've read:

Changing PowerShell's default output encoding to UTF-8

I'm expecting PowerShell give the right output file.

1

There are 1 best solutions below

0
Yatung Yü On

the answer by @mklement0 worked for me.

[Console]::OutputEncoding must match the character encoding used in adb's output. If it is UTF-8, (temporarily) set [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new(). If you make UTF-8 the default encoding system-wide, as shown in the last link in your question, that won't be necessary, but this change has far-reaching consequences. – mklement0

By the way, it's another easy way to fix this as follow. change system area language setting