1

What I expected:

As I execute ebook-convert.exe --list-recipes > "builtin recipes.txt" in Windows cmd, The output file content is exactly right:

Correct output file

The Issue:

But When I try to get the output file in PowerShell 7.3.5, non-English characters don't display correctly.

enter image description here

These nonsense code display following any of the commands I have excuted:

ebook-convert.exe --list-recipes > "builtin recipes.txt"

ebook-convert.exe --list-recipes | out-file -Encoding utf8 "builtin recipes.txt"

ebook-convert.exe --list-recipes | out-file -Encoding unicode "builtin recipes.txt

Note

  1. After the last command, VS Code successfully detected Encoding change from UTF-8 to UTF-16 LE, But the content displayed is still not correct.
  2. It is the same situation when I open the text file in Notepad.
  3. Direct console output ebook-convert.exe --list-recipes in PowerShell is correctly displayed.
  • 1
    try `$OutputEncoding = [System.Console]::OutputEncoding = [System.Console]::InputEncoding = [System.Text.UTF8Encoding]::new()` before running your command. I would also recommend `Set-Content` everyday over `Out-File` – Santiago Squarzon Jul 05 '23 at 02:24
  • 1
    Wow! Setting `$OutputEncoding` worked. Thanks! May I know what this line of code does? Plus why is `Set-Content` better than `Out-File`? – PineapplePie Jul 05 '23 at 02:33
  • [`$OutputEncoding`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.3#outputencoding) ;) whats weird is that you're using latest pwsh version and the default preference is UTF8 already. But glad it worked! – Santiago Squarzon Jul 05 '23 at 02:39
  • As for why `Set-Content` is preferable with _text_ input, see [this answer](https://stackoverflow.com/a/44246434/45375). – mklement0 Jul 05 '23 at 02:42
  • Thanks again! The default does seem to be UTF8 as it is detected by VS Code, though it does not display as expected. I am going to scrutinize it later. – PineapplePie Jul 05 '23 at 02:44
  • @PineapplePie, `$OutputEncoding` doesn't apply here, because no data is being _piped to_ external programs. However, the value of `[System.Console]::OutputEncoding` matters, as it controls how PowerShell interprets the output from external programs, which _invariably_ applies, even with `>`, up to at least PowerShell 7.3.x: output from external programs is _invariably_ decoded into .NET strings first, and then gets _re-encoded_ on output, be it via `>` (in effect an alias of `Out-File`) or via `Set-Content`. In _Windows PowerShell_, unfortunately, these cmdlets' default encodings _differ_. – mklement0 Jul 05 '23 at 02:49

0 Answers0