Is there any difference or drawback when always using $MyInvocation or $PSBoundParameters instead of $PSCmdlet.MyInvocation or $PSCmdlet.MyInvocation.BoundParameters respectively?
I already had a look at the following things:
Searched the internet (obviously)
-
- There the following is written:
$MyInvocation is populated only for scripts, function, and script blocks.
- Furthermore, I know that the
$PSCmdletvariable is only available when using[CmdletBinding()]and aparam ()block. - It looks like these variables were added quite some time ago (PowerShell v2.0?).
Then, I executed the following code within different contexts:
$PSCmdlet.MyInvocation.Equals($MyInvocation) $PSCmdlet.MyInvocation.BoundParameters.Equals($PSBoundParameters)These lines return
truein all the following cases (all files contain[CmdletBinding()]andparam ()):- Directly executed in a script
- When executed in a script via the call operator (
&) - When executed in a module cmdlet
So, is it recommended (and safe) to use $MyInvocation and $PSBoundParameters in every possible situation (i.e. script, function, module, etc.)? And do they always return the same values as the $PSCmdlet.* counterparts (assuming that [CmdletBinding()] is given)?