1

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.

4
  • 1
    The redirection operators map to Out-File, which defaults to UTF16-LE. Pipe your output to Set-Content -Encoding utf8 instead Commented Aug 31, 2023 at 8:21
  • It is probably a different font, not encoding. Commented Aug 31, 2023 at 8:45
  • 1
    Out-File defalts to UTF-8 in my PowerShell 7. @MathiasR.Jessen Commented Aug 31, 2023 at 12:11
  • Please edit your question to improve your minimal reproducible example. In particular, do not use (sole) images of code/data/errors in your minimal reproducible example. Copy the actual text, paste it into the question, then format it as code. Please especially share those Chinese strings (both wrong and right ones)… Commented Aug 31, 2023 at 18:48

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.