Reverse elements via pipeline

dharmatech picture dharmatech · Mar 31, 2014 · Viewed 10.1k times · Source

Is there a function that reverses elements passed via pipeline?

E.g.:

PS C:\> 10, 20, 30 | Reverse
30
20
10

Answer

mjolinor picture mjolinor · Mar 31, 2014

You can cast $input within the function directly to a array, and then reverse that:

function reverse
{ 
 $arr = @($input)
 [array]::reverse($arr)
 $arr
}