Complicated evaluation of $ args in PowerShell

The $ args variable should, by definition, contain all the arguments passed to the script function. However, if I build a pipeline inside my function, the $ args variable evaluates to null. Does anyone know why?

See this example:

function test { 1..3 | % { echo "args inside pipeline: $args" } ; echo "args outside pipeline: $args" }

This is the result when the hello parameter is passed:

PS> test hello
args inside pipeline:
args inside pipeline:
args inside pipeline:
args outside pipeline: hello

Is there a specific reason for this? I know how to get around this, but I wonder if anonye can explain the reason for this.

+3
source share
1 answer

Pipes use $ input. Try the following:

function test { 1..3 | % { echo "args inside pipeline: $input" } ; echo "args outside pipeline: $args" }
+5
source

All Articles