The automatic variable $PSBoundParameters is a hashtable containing the parameters passed to a script or a function.
It only includes the values of parameters that were explicitly supplied by the caller, so it does not include any which have been left at the default value.
$PSBoundParameters can be used to call a subordinate function or cmdlet passing the same parameters - PowerShell will automatically splat the hash table's values instead of having to type each of the parameters:
get-otherthing @PSBoundParameters
Within the script/function the parameters would be defined using a param() statement.
Function DemoFunc1 {
param(
[string]$demoText,
[int]$demoNumber
)
# Display all the passed parameters:
$PSBoundParameters
# or with a switch statement:
switch ($PSBoundParameters.Keys) {
'demoText' { write-output ' A value for demoText was supplied' }
'demoNumber' { write-output ' A value for demoNumber was supplied' }
}
# or looping through all the key/value pairs
foreach($boundparam in $PSBoundParameters.GetEnumerator()) {
"Key={0} Value={1}" -f $boundparam.Key,$boundparam.Value
}
# or Call a second function passing all the parameters plus any extra if needed:
DemoFunc2 @PSBoundParameters -ExtraDemoParam 'Testing 123'
}
Function DemoFunc2 {
param(
[string]$demoText,
[int]$demoNumber,
[string]$ExtraDemoParam
)
Write-Output "$demoText $demoNumber $ExtraDemoParam"
}
You can also check for the existence of a specific key with $PSBoundParameters.ContainsKey('demoText') or $PSBoundParameters['demoText']
If ($PSBoundParameters.ContainsKey('demoText')) {
Write-Output -InputObject "Text has been included as: '$demoText'"
}
$PSBoundParameters can be manipulated like any other hash table, so it is possible to .add or .remove key/value pairs.
$PSBoundParameters.Add('Key','Value')
$PSBoundParameters.Remove('Key')
So to add 'myDescription' and remove 'demoNumber':
$PSBoundParameters.Add('myDescription','Hello world')
$PSBoundParameters.Remove('demoNumber')
“A major advantage of age is learning to accept people without passing judgment” ~ Liz Carpenter
Related PowerShell Cmdlets:
Get-ParameterValues.ps1 - Script to return both Bound Parameters and Default values.
Test-Params.ps1 - function with example that returns both Bound Parameters and Default values.