How to get all arguments passed to function (vs. optional only $args)

26,851

Solution 1

$args returns any undeclared parameters, not optional parameters. So just don't declare parameters.

In PowerShell v2, you can use $PSBoundParameters to get all parameters in a structured way.

Solution 2

$PSBoundParameters gets you all the parameters that were "bound" along with the bound values in a hashtable, it doesn't get you the optional/extra arguments. That is what $args is for. AFAICT the only way to get what you want is to combine the two:

$allArgs = $PsBoundParameters.Values + $args
Share:
26,851
Hadeel Fouad
Author by

Hadeel Fouad

e-mail: alex2k8.stackoverflow [at] gmail [dot] com

Updated on May 04, 2020

Comments

  • Hadeel Fouad
    Hadeel Fouad about 4 years

    $args returns only optional arguments. How can I get all function parameters?