What’s in your PowerShell $PSDefaultParameterValues Preference Variable?

The $PSDefaultParameterValues preference variable, which was introduced in Windows PowerShell version 3.0, provides a mechanism for specifying default values for parameters. I thought I would share what I've added to mine and ask the community to share theirs.

 1$PSDefaultParameterValues = @{
 2    'Out-Default:OutVariable' = 'LastResult'
 3    'Out-File:Encoding' = 'utf8'
 4    'Export-Csv:NoTypeInformation' = $true
 5    'ConvertTo-Csv:NoTypeInformation' = $true
 6    'Receive-Job:Keep' = $true
 7    'Install-Module:AllowClobber' = $true
 8    'Install-Module:Force' = $true
 9    'Install-Module:SkipPublisherCheck' = $true
10}

psdefaultparametervalues1a.jpg

The first one in the list ('Out-Default:OutVariable' = 'LastResult') is one I picked up from Joel Bennett to store the results of the last command in a variable named $LastResult. Since then, I've seen others with similar options except using double underscores for the variable name. What would be really nice is to have the option to save a specific number of the last results in different elements of a variable similarly to the way the $Error automatic variable works.

Many of the other options I've specified are self-explanatory. One of the areas where $PSDefaultParameterValues really shines is for commands that you always forget to set some option for that probably should be the default out of the box. When running ad-hoc commands and piping the results to Export-Csv or ConvertTo-Csv, I almost always forget to specify the NoTypeInformation parameter. That means having to re-run the command again, specifying that option. I use jobs in PowerShell so infrequently that the same is true for specifying the Keep parameter for Receive-Job. Never again though, now that I've specified those options in my $PSDefaultParameterValues preference variable.

The last three options have to do with installing modules from the PowerShell Gallery or other NuGet repositories. I consider those protections to be in place because of the same reasons the execution policy exists in PowerShell. To prevent users from unknowingly doing something. These three options take the safety off because I know what I'm doing and I'm willing to take the associated risks, although I definitely wouldn't add those options to a normal user's system.

The options specified for $PSDefaultParameterValues don't persist between PowerShell sessions so I've added them to my PowerShell profile. They can be cleared for the current session using the following command.

1$PSDefaultParameterValues.Clear()

psdefaultparametervalues2a.jpg

Although they can be cleared altogether as shown in the previous example, maybe you only want to disable them while running a command or two. There's an option to disable the specified values without clearing them.

1$PSDefaultParameterValues.Add('Disabled', $true)

psdefaultparametervalues3a.jpg

To re-enable them, simply remove the Disabled option.

1$PSDefaultParameterValues.Remove('Disabled')

psdefaultparametervalues4a.jpg

What's in your $PSDefaultParameterValues preference variable?

For more information about $PSDefaultParameterValues, see the About Parameters Default Values help topic.

µ