Last week I shared a little nugget about making PowerShell life easier by using the built-in variable, $PSDefaultParameterValues. This is a special hashtable where you can define default parameter values for any PowerShell command. This means any PowerShell script or function that has defined parameters AND uses [cmdletbinding()]. If you have a simple function that only uses a Param() block, $PSDefaultParameterValues won't work for that function. I recommend that all PowerShell functions use [cmdletbinding()] so hopefully, this won't be an issue. Since I received some positive feedback and a few questions on the previous post, I thought a quick follow-up might be in order.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Not Persistent
First, anything you put in $PSDefaultParameterValues is not persistent across PowerShell sessions. This is nothing more than another type of variable. As you should know, variables only exist for the duration of your PowerShell session. Or to put a more technical point on it, for the duration of the PowerShell scope. If you have parameter defaults you want to use all the time, put defining statements in your PowerShell profile script. You can any use any of these syntaxes.
$PSDefaultParameterValues["get-ciminstance:classname"]="win32_bios"
$PSDefaultParameterValues.Add("get-ciminstance:Verbose",$True)
$PSDefaultParameterValues."Export-Clixml:Encoding" = "UTF8"
Exporting and Importing
Another profile-related option you might consider is to define all the default values you want, then export $PSDefaultParameterValues to a file.
$PSDefaultParameterValues | Export-Clixml c:\work\psdefaults.xml
I would recommend using Export-Clixml because it captures the most information and makes it very easy to import into a new session.
I would definitely not use a CSV file because some of your default values might be nested objects or arrays. JSON might be a little better, but you would need to rebuild $PSDefaultParameterValues from it. And it probably won't handle values like SecureStrings or credentials. On Windows platforms, Export-Clixml will easily and securely store this type of data. Of course, you still want to take adequate precautions to protect the file.
Using and Not Using
The commands I ran earlier set a pair of default parameter values.
I didn't have to do anything. You can always specify other parameters or behavior.
As I have mentioned, $PSDefaultParameterValues is special. You can temporarily disable it by adding a Disabled key.
$PSDefaultParameterValues.disabled = $True
The variable is still defined in my session but ignored. When I set Disabled to $False, the defaults will be used again. You can also completely clear the variable.
I strongly recommend you read the about_parameters_default_values help topic to learn even more.
I am certainly passing it a hashtable even as addition (+=). Or directly assigning thrue dot nottation. And almost always useful for *-DNSServerResourceRecord:ZoneName in profile for us lazy admins. Dont forgot the woking wildcards!
I use a lot of wildcards in my settings.