I have a perl script that produces utf8 output. I tried using Set-Content to write a utf8 file as suggested by Powershell overruling Perl binmode?.

perl -S testbinmode.pl | Set-Content "binmode.txt" -Encoding Byte

produces the error

"Set-Content : Cannot proceed with byte encoding. When using byte encoding the content must be of type byte."

perl -S testbinmode.pl | Set-Content "binmode.txt" -Encoding UTF8

doesn't produce an error message, but it doesn't write a correct utf8 file either.

The output of the perl script is displayed correctly in the Powershell window. What is the correct way to write that output to a utf8-encoded file?


Update: I have seen many responses to this and similar problems, here at the link referenced above, and at https://stackoverflow.com/questions/40098771/changing-powershells-default-output-encoding-to-utf-8. None of them appear to work, leading me to believe that not one has actually been tested. A tested method for redirecting UTF8 text output from a CLI program to a file is desired. Thanks.

Here is the perl test script:

use strict;
use warnings;
use utf8;
binmode(STDOUT, ":utf8");
print("The Crüxshadows");
  • Have the perl script output to a text file then run Set-Content on that. Which while similar to what your doing isn't exactly the same – Ramhound Sep 4 '17 at 6:06
  • perl -S testbinmode.pl >binmode.txt...Set-Content "binmode.txt" -Encoding UTF8...produces cmdlet Set-Content at command pipeline position 1 Supply values for the following parameters: Value[0]:...how do I proceed?...Why does this box close and post when I attempt to separate my response into multiple lines? – Freon Sandoz Sep 4 '17 at 6:36
  • Update your question your unformatted comment can't be read. I don't answer question asked in a comment or consider any information contained within an comment when submitting an answer – Ramhound Sep 4 '17 at 7:44
  • try this: $utf8 = New-Object System.Text.utf8encoding and then use it as your encoding: perl -S testbinmode.pl | Set-Content "binmode.txt" -Encoding $utf8 – SimonS Sep 4 '17 at 10:41
  • also: I guess the error message says that there is no content to write, or it doesn't know where to write it to – SimonS Sep 4 '17 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.