views:

42

answers:

1

In a Unix shell script, to call command 'foo' on the original arguments to the script, you just write

foo $@

In Powershell, $args is an array of (typically) strings holding the current script's arguments. If you write

foo $args

will the same thing happen as in bash or other typical Unix shell script interpreters?

+1  A: 

You want to use argument splatting which is new in PowerShell 2.0. The variable used for splatting can be either an array (args are bound positionally) or a hashtable (keys map to parameter names and their values provide the argument). If you don't declare any parameters then use @args. The problem with attempting to use $args is that it will be sent as an array to the first parmeter rather than splatted across all the parametets. However many folks do declare parameter. In this case you want to use @PSBoundParameters e.g.:

foo @PSBoundParameters
Keith Hill
Please note, this is not exactly the same as passing all the parameters along. Switches will come out as boolean parameters.
JasonMArcher
It is *effectively* the same for all the "bound" parameters. That is, PowerShell will apply the bool to the downstream switch parameter so you wouldn't realize it went through a bool. Besides [switch] is just a parsing construct sitting on top of a bool anyway. Now what `@PSBoundParameters` doesn't get you is any unbound parameters.
Keith Hill