PSBoundParameters – Credentials

PSBoundParameters – Credentials

The PSBoundParameter is an amazing tool that will clear your mind of worries about non-mandatory parameter sets. In my Super Help Desk module I use the credential flag. Inside this flag, I tell the system to use the credentials provided or the currently running credentials.

I do this by testing the $PSBoundParameter.ContainsKey(‘Credential’). What this basically does is test the bounded parameters coming in to see if it contains a keyword. In this case I’m looking for Credential.

if ($PSBoundParameters.ContainsKey('Credential')) {
    Do-Something -Credential $Credential
} else {
    Do-Something
}

By having the flag set inside the parameter area not set to mandatory, you can test to see if they want to use a special set of credentials or not.

[Parameter(HelpMessage = "Allows for custom Credential.")][System.Management.Automation.PSCredential]$Credential

You can use other parameters as well but the credentials are one of the best usages for $PSboundparameter.containskey(). I do hope you enjoy using this little trick in your future scripts! Let me know what you create.