Search notes:

PowerShell: The automatic variable $PSCmdlet

The automatic variable $psCmdLet is available in scripts and functions that were declared with [cmdletBinding()] param(…) and represents the currently running cmdlet or function.

Type of $psCmdLet

The type of $psCmdLet is System.Management.Automation.PSScriptCmdlet.
function typeof-psCmdlet {
  [cmdletBinding()] param()

   echo "type of `$psCmdlet is $($psCmdlet.GetType().FullName)"
}

typeof-psCmdlet
#
# type of $psCmdlet is System.Management.Automation.PSScriptCmdlet
Github repository about-PowerShell, path: /language/variable/automatic/psCmdLet/getType.ps1

Not all functions have a defined $psCmdLet

The following example tries to demonstrate that $psCmdLet is assigned a value in a function
#
#  Defining a few functions
#
function func_1 {
   param($param)
   write-output "psCmdlet = $psCmdlet, param = $param"
}

#
#  Define another function with the only difference
#  that is has the cmdletBinding attribute:
#
function func_2 {
  [cmdletBinding()]
   param($param)
   write-output "psCmdlet = $psCmdlet, param = $param"
}

#
#  This function does not have a a [cmdletBinding()] instruction.
#  However, the param() statement encloses a [parameter(...)] thingy
#  which also causes $psCmdlet to be defined:
#
function func_3 {
   param([parameter()] [string] $param)
   write-output "psCmdlet = $psCmdlet, param = $param"
}


#
#  Invoking the functions
#

func_1 one
#
#  psCmdlet = , param = one

func_2   two
#
#  psCmdlet = System.Management.Automation.PSScriptCmdlet, param = two

func_3 three
#
#  psCmdlet = System.Management.Automation.PSScriptCmdlet, param = three
Github repository about-PowerShell, path: /language/variable/automatic/psCmdLet/cmdletBinding.ps1

See also

The influence of System.Management.Automation.CmdletBindingAttribute
$psCmdLet.parameterSetName contains the name of the parameter set that was used when a cmdLet was invoked (see also Param(Parameter(ParameterSetName=…)))
System.Management.Automation.PagingParameters
Other automatic variables

Index