powershellpipelinenewlineline-endings

PowerShell pipeline adds newline when passing to the next command


I'm trying the the Rust version of coreutils on my Windows 10 system. I renamed coreutils.exe to co.exe so that I can type less. od is the Windows version of the Linux command od [from linux man site = od - dump files in octal and other formats]. Here is my test.

PS C:\> co printf "asdf" | od -c
0000000   a   s   d   f  \r  \n
0000006
PS C:\> co printf "asdf" | Set-Content -NoNewline out.txt; od -c out.txt
0000000   a   s   d   f
0000004
PS C:\>

As you can see, PowerShell pipeline adds newline when passing to the next command. How to disable this behavior?


Update

I also tested on a Linux system.

[user@server ~]$ printf abcd | od -c
0000000   a   b   c   d
0000004
[user@server ~]$

Also, here is the result on a native cmd.exe.

C:\>co printf "asdf" | od -c
0000000   a   s   d   f
0000004

C:\>

As you can see, both the Linux version and cmd.exe don't add such newline characters. @JosefZ mentioned that WSL also adds such character, which I haven't tested.


Update2

I just found the Format-Hex CmdLet from @Richard's answer. Here is another test:

PS C:\> co printf "asdf" | Format-Hex


           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   61 73 64 66                                      asdf


PS C:\>

I'm still confused about the various behaviors.


Solution

  • tl;dr


    In Windows PowerShell (the legacy, ships-with-Windows, Windows-only edition of PowerShell whose latest and final version is 5.1), as well as in - now obsolete - PowerShell (Core) 7 versions up to 7.3.x, the following behaviors apply:

    The workaround is to delegate the operation to cmd.exe, whose | operator always acts as a raw byte conduit: