Search code examples
powershellpipeline

How to add to a pipeline in PowerShell?


Let's say I have two files, f1.txt and f2.txt. f1.txt has some corrections that need to be made, after which both files will need to be processed together. How do I merge the output of f1.txt corrections to the pipeline with f2.txt data?

Here is an illustration:

Get-Content f1.txt |
% {
    $_ #SOME OPERATION
} # How do I merge this output into the next pipeline?
Get-Content f2.txt |
% {
    #COMBINED OPERATIONS on f1.txt output and f2.txt
} > output.txt

I understand I can save the first operation into a temporary file and read from it again for the combined operation:

...
} > temp.txt
Get-Content temp.txt, f2.txt |
...

But is there a way to do it without creating buffer files?


Solution

  • You can wrap multiple commands in single SciptBlock and invoke it:

    & {
        Get-Content f1.txt |
        % {
            $_ #SOME OPERATION
        }
        Get-Content f2.txt
    } |
    % {
        #COMBINED OPERATIONS on f1.txt output and f2.txt
    } > output.txt