2

if I redirect the output of a program (svn in my case) in powershell to a file, the output is in UTF-16. Can I get it as UTF-8 instead? how?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • 1
    How do you redirect output at the moment? By `>` operator, piping to `Out-File`, some other way? – vonPryz Jan 24 '19 at 13:47
  • @vonPryz Just > – Stefano Borini Jan 24 '19 at 13:49
  • 1
    @StefanoBorini use [Out-File](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-6) with the encoding you want. The default is UTF16. Windows strings are UTF16 so the default makes sense – Panagiotis Kanavos Jan 24 '19 at 13:50
  • @PanagiotisKanavos is there a way to make > default to utf-8? – Stefano Borini Jan 24 '19 at 13:53
  • 2
    @StefanoBorini check the [about_Redirection](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection?view=powershell-6) help page. It explains the redirection options and operators – Panagiotis Kanavos Jan 24 '19 at 13:53
  • @StefanoBorini why? That would break every script in production. Windows strings *are* UTF16, which means UTF16 text can be read without conversions or risking encoding errors. Do you have a specific issue with encodings perhaps? Are you trying to feed the output to a non-Unicode program or copy it to a Linux machine? – Panagiotis Kanavos Jan 24 '19 at 13:53
  • @PanagiotisKanavos because if I redirect with svn diff a patch, and then have to upload the patch to something that does not understand the dumb windows encoding, I don't want to tell everybody to just pipe it through out-file and specify the encoding to utf-8 every time, because it's guaranteed they will screw it up more often than not – Stefano Borini Jan 24 '19 at 13:56
  • @StefanoBorini use Out-File. Or configure svn to use UTF8. I'm pretty sure svn configures this during installation but I've moved to git 5 years ago. In a mixed Windows/Linux environment with the `git` server hosted on Linux too – Panagiotis Kanavos Jan 24 '19 at 14:03
  • @StefanoBorini check [SVN Error: Can't convert string from native encoding to 'UTF-8'](https://stackoverflow.com/questions/2116718/svn-error-cant-convert-string-from-native-encoding-to-utf-8) too. – Panagiotis Kanavos Jan 24 '19 at 14:08
  • Do you need this for `svnadmin dump` and `svnadmin load`? – bahrep Jan 24 '19 at 14:41

1 Answers1

-1

After you get the output in UTF-16, you can then convert it to UTF-8 maybe? The way to do it can be done is answered here.

The meaningful part in your case is

Set-Content -Encoding UTF8
Chen Chen
  • 172
  • 1
  • 12