What's the equivalent of xargs in PowerShell?

PowershellXargs

Powershell Problem Overview


The POSIX-defined xargs command takes all of the items it receives from standard input and passes them as command-line arguments to the command it receives on it's own command line. E.g: grep -rn "String" | xargs rm.

What's the equivalent in PowerShell?

The following questions all ask this:

but there is no correct answer because all the answers either use ForEach-Object, which will process items one-at-a-time (like xargs -n1) which gets the desired result for the examples given, or store the intermediate result in a variable, which offends my functional commandline-fu.

Powershell Solutions


Solution 1 - Powershell

There are two ways that I've found. The first is probably more idiomatic PowerShell, and the second is more true to the pipe-based spirit of xargs.

As an example, let's say we want to pass all our cat pics to myapp.exe.

Method #1: Command substitution

You can do something similar to using $(command substitution) in sh by embedding your pipeline in the command string:

&"myapp.exe" @(Get-ChildItem -Recurse -Filter *.jpg | Another-Step)

The @(...) creates an array from the command inside it, and PowerShell automatically expands arrays passed to & into seperate command-line parameters.

However, this does not really answer the question, because it will only work if you have control over the command you want to pass to, which may not be the case.

Method #2: True piping

You can also construct a "double pipeline" by having a sub-expression to pipe your objects, collecting them to an array, and then piping the array to your final command.

,@(Get-ChildItem -Recurse -Filter *.jpg | Another-Step) | %{&"myapp.exe" $_}

The @(...) as before collects the items into an array, and the array is then piped to the final command which is invoked using % (ForEach-Object). Ordinarily, this would then loop over each item individually, because PowerShell will automatically flatten the array when it's piped, but this can be avoided by prepending the , operator. The $_ special variable is then used as normal to embed the passed array.

So the key is to wrap the pipeline you want to collect in ,@(...), and then pipe that to something in %{...}.

Solution 2 - Powershell

I've been using this filter for the basic xargs execution.

filter xargs { ($h,$t) = $args; & $h ($t + $_) }

which is roughly equivalent to:

filter xargs { & $args[0] ($args[1..$args.length] + $_) }

Examples

 docker ps -q | xargs docker stop

 gem list | % { $_.split()[0] } | xargs gem uninstall -aIx

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsimonwoView Question on Stackoverflow
Solution 1 - PowershellsimonwoView Answer on Stackoverflow
Solution 2 - PowershellIan DavisView Answer on Stackoverflow