Pipes in PowerShell

Discussion in 'Programming/Html' started by Anarion, Sep 30, 2015.

  1. Anarion

    Anarion Ancient Guru

    Messages:
    13,599
    Likes Received:
    387
    GPU:
    GeForce RTX 3060 Ti
    Any PowerShell gurus around?

    I just can't figure out how to pipe command line program output to another command line program... What would be the powershell way to achieve this:
    Code:
    ffmpeg.exe -i "input.flac" -f wav {more args...} - | lame.exe {args go here} - "output.mp3"
    
    I.e. FFmpeg reads file and send the output directly to Lame through pipeline.
     
    Last edited: Sep 30, 2015
  2. lmimmfn

    lmimmfn Ancient Guru

    Messages:
    10,491
    Likes Received:
    194
    GPU:
    4070Ti
    Is that even possible? I mean in linux the binary output of ffmpeg would have to go to std out which wouldn't normally make sense ( off the top of my head I can't think of any Linux commands that would send binary to std out unless its a user running e.g. cat on a binary file )
     
  3. Anarion

    Anarion Ancient Guru

    Messages:
    13,599
    Likes Received:
    387
    GPU:
    GeForce RTX 3060 Ti
    Oh it totally works in linux and works in windows batch scripts too.
     
  4. mbk1969

    mbk1969 Ancient Guru

    Messages:
    15,597
    Likes Received:
    13,606
    GPU:
    GF RTX 4070
    Code:
    ffmpeg.exe -i "input.flac" -f wav {more args...} - | foreach { lame.exe $_ - "output.mp3" }
    Am I correct assuming that ffmpeg.exe outputs one string which one you should pass to the lame.exe ?

    Common syntax for cases when first program outputs one single string:
    Code:
    $var = some1.exe
    some2.exe -arg $var
    
    # or
    
    some1.exe | foreach { some2.exe -arg $_ }
    In cases when first program outputs more than one string syntax is:
    Code:
    some1.exe | foreach { some2.exe -arg $_ }
    $_ - stands for current value passed through pipeline.
     
    Last edited: Oct 2, 2015

  5. Anarion

    Anarion Ancient Guru

    Messages:
    13,599
    Likes Received:
    387
    GPU:
    GeForce RTX 3060 Ti
    It doesn't seem to work. I think the PS escapes the data the first program is churning out or something. Oh well... It may not be possible to do this with PS.

    Also running command line applications in PS is a small pain in the arse.

    This works well but piping data is not possible as far as I know:
    Code:
    start -FilePath $pathtoapp -Args $appargs -NoNewWindow -Wait
    
    Code:
    $app = "S:\something\something.exe"
                
    #Works:
    &($app) -i something.txt -o something2.txt
    
    #Doesn't work because it thinks that there's one arg ("-i something.txt -o something2.txt")
    $args = "-i something.txt -o something2.txt"
    &($app) $args
    
     
  6. mbk1969

    mbk1969 Ancient Guru

    Messages:
    15,597
    Likes Received:
    13,606
    GPU:
    GF RTX 4070
    Describe here what task you are seeking.

    I tried the following:
    Code:
    $qq = C:\Tools\SysinternalsSuite\autorunsc.exe
    $qq
    
    Code:
    $qq = C:\Tools\SysinternalsSuite\autorunsc.exe -b
    $qq
    
    Code:
    $qq = &("C:\Tools\SysinternalsSuite\autorunsc.exe") -b
    $qq
    
    Code:
    $aa = "-b"
    $qq = &("C:\Tools\SysinternalsSuite\autorunsc.exe") $aa
    $qq
    

    And PS prints me all strings that program did output and which PS did save into variable.

    You can`t use name "args" for variable because it has predefined meaning: $args - input arguments for a script, function, script block.
    When you assign any value to $args PS doesn`t show error - it just silently ignores the assignment.
     
    Last edited: Oct 11, 2015

Share This Page