Mr. Kennedy Mr. Kennedy - 3 months ago 29
PowerShell Question

Win7 Powershell echo > text file = Ruby2.2: invalid multibyte char (UTF-8)?

Can I use echo from the powershell command line to create text files which Ruby can parse?

>>> echo "# foobar" > echo_test.rb
>>> ruby echo_test.rb
echo_test.rb:1: invalid multibyte char (UTF-8)


Notepad++ reports the encoding as UCS-2 Little Endian. I also tried echo'ing with the the header options mentioned in this thread:
invalid multibyte char (UTF-8) Error, Ruby

e.g.

>>> echo "# encoding: utf-8" > echo_test.rb


and got the same error message.

Answer

You can change the encoding if you pipe it into Out-File

Example:

'# foobar' | Out-File -FilePath echo_test.rb -Encoding ascii
ruby echo_test.rb