DevOps | Scripts | Automation

Powershell

How to use $PSBoundParameters in PowerShell?

What is $PSBoundParameters in PowerShell?

$PSBoundParameter is the automatic variable in PowerShell, used to store the parameter name and value in the hashtable format in Key-Value pair manner.

Example:

param(
    [Parameter(Mandatory)]
    [String]$State,
    [Parameter(Mandatory)]
    [String]$City,
    [Parameter(Mandatory)]
    [int]$CityCode
)

Write-Output "`nDisplaying PSBoundParameters`n"

$PSBoundParameters

Output:

So if the $PSBoundParameters is the Hashtable, we can access its keys using $PSBoundParameters.Keys and Values using $PSBoundParameters.Value.

To access its individual Key to retrieve its value,

$PSBoundParameters.State
$PSBoundParameters['State']

$PSBoundParameters.City
$PSBoundParameters['City']