Search code examples
powershellpipe

How to migrate chained commands from command line to powershell?


I try to migrate a chain of commands from command line to powershell.

My original code extracts a .tar.gz archive using 7zip like this:

7z x -so archive.tar.gz | 7z x -si --ttar

Now, I try to do the same in powershell. I've tried like this, but all I get is to throw the first command result to the shell:

Start-Process -NoNewWindow $7z -ArgumentList "x -so `"archive.tar.gz`"" | Start-Process -NoNewWindow $7z -ArgumentList "x -si -ttar"

How is this achieved in powershell?


Solution

  • When piping binary data in PowerShell, we need to ensure we're not inadvertently converting it to text. Most clever approch would be

    cmd /c "7z x -so archive.tar.gz | 7z x -si -ttar"
    

    cmd handles binary streams properly by default