Passing around command line $args in powershell , from function to function Passing around command line $args in powershell , from function to function powershell powershell

Passing around command line $args in powershell , from function to function


In PowerShell V2, it's trivial with splatting. bar just becomes:

function bar { foo @args }

Splatting will treat the array members as individual arguments instead of passing it as a single array argument.

In PowerShell V1 it is complicated, there's a way to do it for positional arguments. Given a function foo:

function foo { write-host args0 $args[0] args1 $args[1] args2 $args[2]   }

Now call it from bar using the Invoke() method on the scriptblock of the foo function

function bar { $OFS=',';  "bar args: $args";  $function:foo.Invoke($args) }

Which looks like

PS (STA) (16) > bar 1 2 3bar args: 1,2,3args0 1 args1 2 args2 3

when you use it.


# use the pipe, Luke!file1.ps1---------$args | write-host$args | .\file2.ps1    file2.ps1---------process { write-host $_ }