echo “string” > file in Windows PowerShell appends non-printable character to the file

Question:

In Windows PowerShell:

In Cygwin:

I want a simple “string” in the file. How do I do it? I.e., when I say cat file.txt I need only “string” as output. I am echoing from Windows PowerShell and that cannot be changed.

Answer:

Try echo "string" | out-file -encoding ASCII file.txt to get a simple ASCII-encoded txt file.

Comparison of the files produced:

will produce a file with the following contents:

however

will produce a file with the following contents:

(Byte order mark FF FE indicates the file is UTF-16 (LE). The signature for UTF-16 (LE) = 2 bytes: 0xFF 0xFE followed by 2 byte pairs. xx 00 xx 00 xx 00 for normal 0-127 ASCII chars

Source:

echo “string” > file in Windows PowerShell appends non-printable character to the file by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply