Pipes in PowerShell

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

  1. Anarion

    Anarion Ancient Guru

    Messages:
    12,921
    Likes Received:
    2
    GPU:
    EVGA GeForce GTX 1070 ACX
    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,245
    Likes Received:
    6
    GPU:
    MSI 980 Ti
    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:
    12,921
    Likes Received:
    2
    GPU:
    EVGA GeForce GTX 1070 ACX
    Oh it totally works in linux and works in windows batch scripts too.
     
  4. mbk1969

    mbk1969 Ancient Guru

    Messages:
    3,565
    Likes Received:
    8
    GPU:
    GeForce GTX 970
    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:
    12,921
    Likes Received:
    2
    GPU:
    EVGA GeForce GTX 1070 ACX
    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:
    3,565
    Likes Received:
    8
    GPU:
    GeForce GTX 970
    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