airfoyle
airfoyle

Reputation: 453

What is the Powershell analogue of $@ used to call another command?

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?

Upvotes: 4

Views: 2208

Answers (1)

Keith Hill
Keith Hill

Reputation: 201972

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

Upvotes: 3

Related Questions