You can do this with dynamic parameters (documented in "about_Functions_Advanced_Parameters"), and this is likely to be the "right" solution. But depending on the complexity of your arguments, you can achieve the same effect in several ways.
You can call your function recursively with the correct location of the arguments:
function ff($a, $b) { if (!$PSBoundParameters.ContainsKey('b')) { ff -b $a; return }
It leads to:
PS C:\Users\Droj> ff one two a: one b: two PS C:\Users\Droj> ff one a: b: one PS C:\Users\Droj> ff a: b:
But this does not allow you to define different types or validation of arguments. So, if you need it, another option is to wrap the main logic in a script block and pass the correct parameters based on what is being passed. Something like that:
function ff() { param($a, $b) $go = { param($a, $b) "a: $a" "b: $b" } if (!$PSBoundParameters.ContainsKey('b')) { &$go -b $a } else { &$go @PSBoundParameters } }
..., which leads to the same conclusion.
Hope this helps!
source share