Had a talk with a colleague about whether to use IsPresent or PSBoundParameters to validate if a Switch or Parameter was used in a Script or Function. In this blog, I will explain what they do and how you can use both.
What do PSBoundParameters do?
“$PSBoundParameters in PowerShell is an automatic variable that is populated based on the parameters used with a function. It is a hashtable whose keys contain the parameter name used, and values contain the argument/value passed in. This can come in handy when you want to perform different tasks based on the parameters used in a function.”
Source: https://gnja.io/blog/using-psboundparameters-in-powershell
What does IsPresent do?
“Returns true if the parameter was specified on the command line, false otherwise.”
Use cases
IsPresent
I use IsPresent in scripts to check whether a Switch (Boolean/$True or $False) was used so that I can use that in lines like this:
Install-PSResource $Module.Name -Prerelease:$Prerelease.IsPresent -AcceptLicense:$true -Scope:$Scope -ErrorAction Stop -WhatIf:$WhatIf.IsPresent -Verbose:$Verbose.IsPresent -SkipDependencyCheck:$true -Reinstall:$true
In the example above (from my Update PowerShell modules blog post), I have a few Switches in that Function that allow you to install a pre-release version, run the Function in a what-if scenario, and enable Verbose output. This looks like this:
Function Update-Modules {
param (
[switch]$Prerelease,
[string]$Name = '*',
[string[]]$Exclude,
[ValidateSet('AllUsers', 'CurrentUser')][string]$Scope = 'AllUsers',
[switch]$UpgradeToPSResource,
[switch]$WhatIf,
[switch]$Verbose
)
By default, when a Switch Parameter is not used, it’s $False, which configures the Parameters of Install-PSResource (in this case) to their default values. If the Switch Parameter is used, it becomes $True, enabling the Install-PSResource Parameters to run in a WhatIf scenario.
PSBoundParameters
This is more flexible; it has all the values of Switches, Strings, etc. For example, I added $PSBoundParameters to my Update-Modules Function, and used Return to exit it after displaying instead of running the Function completely)
function Update-Modules {
param (
[switch]$Prerelease,
[string]$Name = '*',
[string[]]$Exclude,
[ValidateSet('AllUsers', 'CurrentUser')][string]$Scope = 'AllUsers',
[switch]$UpgradeToPSResource,
[switch]$WhatIf,
[switch]$Verbose
)
$PSBoundParameters
Return
When I run Update-Modules with no Parameters, it just exits to my console prompt, no values?! That’s right 🙂 Because I didn’t specify them, running the Update-Modules Function and specifying some Parameters, it will return this:

Now that you specified the Parameters, they will be displayed by running $PSBoundParameters. You can then use that HashTable to query for the values that you can use in your script. For example:
Install-PSResource $PSBoundParameters.Name -Prerelease:$Prerelease.IsPresent -AcceptLicense:$true -Scope:$PSBoundParameters.Scope -ErrorAction Stop -WhatIf:$PSBoundParameters.WhatIf -Verbose:$PSBoundParameters.Verbose -SkipDependencyCheck:$true -Reinstall:$true
The example above is the same command-line as the one I used with the IsPresent values, but it has been updated with the values returned to the PSBoundParameters HashTable.
Which one is better?
Like all IT things, it depends 😉 IsPresent, for me, is perfect for determining whether a value is $True or $False when using Switches. But PSBoundParameters are nice because they store all specified Parameters and Values passed, but they do not show you the values you specified as a default value (For example, [string]$Name = ‘*’ will not show Name in the PSBoundParameters HashTable.
More information
For more information on how to use PSBoundParameters, check out these links (IsPresent is pretty much clear, I guess 😉 )
- https://bartpasmans.tech/psboundparameters-the-variable-flight-recorder-your-scripts-deserve/
- https://ss64.com/ps/psboundparameters.html
Wrapping up
And that’s how you can check if Switches were used or if Values were passed to your script or Function. Have a lovely weekend and… It’s PSConfEU time next week, will have a write-up about that next Friday!