Best Practice: You should use the automatic variable $PSBoundParameters instead of just specifying the variable name.
Explanation:
The automatic variable $PSBoundParameters is a hashtable containing the parameters passed to a script or a function
By using the $PSBoundParameters, you avoid some possible confusion or unexpected behavior, and also make the code clearer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
function Get-Something { Param ( [switch]$Verbose ) # Bad if ($Verbose) { Do-Something } # Good (Alternative 1) if ($PSBoundParameters['Verbose']) { Do-Something } # Good (Alternative 2) if ($PSBoundParameters.ContainsKey('Verbose')) { Do-Something } # Good (Alternative 3) switch ($PSBoundParameters.Keys) { 'Verbose' { Do-Something } } } |