0

I haven't found a way to properly generate files with accentuated character when forced to use redirection in a powershell shell.

example, this: curl https://www.microsoft.com/fr-ca/ | out-file test.txt will produce result like this in the file(the right result should be "productivité"

productivit├®

I tried many variations with the -encoding flag, like out-file test.txt -Encoding utf8BOM, but they all gave bad results too

Using the CMD shell is not an option as I require to run in a powershell shell.

Any ideas how to do this properly?

Kinwolf
  • 755
  • 2
  • 14
  • 24
  • if you get the expected results with CMD, then you should use the same encoding in powershell. Just specify -Enconding oem – mnieto Nov 17 '21 at 14:19
  • 2
    Try `[Console]::OutputEncoding = [System.Text.Encoding]::UTF8` before calling curl. Works for me on PS 7.2. – zett42 Nov 17 '21 at 14:40
  • Curl is causing me to get the same encoding. You can use the available PowerShell tools to do this with the desired results and without changing the console encoding `(iwr https://www.microsoft.com/fr-ca/).Content | Out-File test.txt` [Invoke-WebRequest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.2) - * Works on PowerShell 7.1.5 and 5.1. – Ash Nov 17 '21 at 15:47
  • 1
    In short: While the output from external programs may _print_ OK to the _console_, when it comes to _capturing the output in a variable or redirecting it_, PowerShell decodes the output into .NET strings, using the character encoding stored in `[Console]::OutputEncoding`. Therefore, you may have to (temporarily) set `[Console]::OutputEncoding` to the encoding used by the external program. See [this answer](https://stackoverflow.com/a/58438716/45375) to the linked duplicate for details. – mklement0 Nov 17 '21 at 15:52
  • For Information: curl from a command line is not the same as curl from powershell, the latter is just an alias for Invoke-WebRequest – David Martin Nov 17 '21 at 15:55
  • As a pragmatic shortcut, you could also use `cmd /c 'curl https://www.microsoft.com/fr-ca/ > test.txt'`, taking advantage of the fact that `cmd.exe` passes the raw output through to the output file. – mklement0 Nov 17 '21 at 15:56
  • 2
    @DavidMartin, that is true for _Windows PowerShell_ (where you need to use `curl.exe` if you truly want to call the external program), but no longer for _PowerShell (Core) 7+_, where the `curl` alias has been removed (so as not to shadow `curl.exe`). – mklement0 Nov 17 '21 at 15:58
  • You can use -outfile to save it, but it looks like a plain ascii html file to me. – js2010 Nov 17 '21 at 18:24
  • Many comments have a working solution, thanks! None of them are as clean as I had hoped but it brings solutions to many use-case, either from the shell, or from a script. `[Console]::OutputEncoding = [System.Text.Encoding]::UTF8` seems to fix it in the shell for about all the programs I tried, that's a nice one to keep :) And inside script, indeed I should use Invoke-Webrequest instead. Somehow, I always forget about that one and always use curl or wget. – Kinwolf Nov 18 '21 at 16:00

0 Answers0