Home > Enterprise >  Avoid unexpected characters in powershell stream-to-file output
Avoid unexpected characters in powershell stream-to-file output

Time:10-01

I would expect the following powershell command:

echo 'abc' > /test.txt

to fill the file /test.txt with exactly 3 bytes: 0x61, 0x62, 0x63.

If I inspect the file, I see that its bytes are (all values are hex):

ff fe 61 00 62 00 63 00 0d 00 0a 00

Regardless of what output I try to stream into a file, the following steps apply in the given order, transforming the resulting file contents:

  1. Append \r\n (0d 0a) to the end of the output
  2. Insert a null byte (00) after every character of the output
  3. Prepend ff fe to the entire output

Transformation #1 is relatively tolerable, but the other two transformations are a real pain.

How can I stream content more precisely (without these transformations) using powershell?

Thanks!

CodePudding user response:

Try this

'abc' | out-file -filepath .\test.txt -encoding ascii -nonewline

should get you 3 bytes instead of 12

61 62 63
  • Related