Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing standard output from native app with Utf8 characters

I'm trying to capture standard output from npm that I run from PowerShell. Npm downloads packages that are missing and outputs appropriate tree.

What it looks like:

enter image description here.

That's correct output.

When I try to do the same from PowerShell and capture the result, I'm not able to get the same characters:

Captured via Tee-Object

It's the same when I use

  • gpm install | Tee-Object -FilePath or
  • gpm install | Out-File -FilePath .. -Encoding Unicode # or Utf8
  • $a = gpm install

When I redircect in cmd.exe output to a file, the content looks like this: enter image description here

How can I capture the output correctly in PowerShell?

like image 621
stej Avatar asked Nov 01 '25 03:11

stej


1 Answers

PowerShell is an object-based shell with an object-based pipeline, but for native applications the pipeline is byte-stream-based. So PowerShell has to convert from/to a byte stream when it passes data from/to a native application. This conversion happens even when you pipe data from one native application to another or redirect a native application's output to a file.

When PowerShell receives data from a native application, it decodes the byte stream as a string, and splits that string by the newline character. For decoding byte streams to strings PowerShell uses the console's output encoding: [Console]::OutputEncoding. If you know that your application use a different output encoding, you can explicitly change the console's output encoding to match your application's:

[Console]::OutputEncoding=[Text.Encoding]::UTF8

When PowerShell passes data to a native application, it convert objects to strings using the encoding specified in the $OutputEncoding preference variable.

like image 132
user4003407 Avatar answered Nov 03 '25 22:11

user4003407