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.

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.

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.

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

What’s in your $PSDefaultParameterValues preference variable?

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

Thoughts, questions, comments? Please post them as a comment to this blog article.

µ

4 Comments

  1. mikeshepard

    Nice article and I agree with most of your choices. I don’t tend to use PSDefaultParameters, but I might start after reading this.

    Might be worthwhile to mention how to override these if you need to (especially switches which aren’t as obvious).

    Reply
  2. Oliv

    $PSDefaultParameterValues = @{
    ‘*:Encoding’ = ‘utf8’
    ‘Install-Module:Scope’ = ‘CurrentUser’ # often i’m testing module for me without impacting anyone
    ‘Test-NetConnection:InformationLevel’ = ‘Quiet’ # nbo progress bar dureing execution
    ‘Export-Csv:NoTypeInformation’ = $true # this could be by default
    ‘Export-Csv:delimiter’ = “;” # cause it’s easier to open in excel (Im’ french, and the default is 😉
    ‘ConvertTo-Csv:NoTypeInformation’ = $true # always
    ‘Write-EZlog:LogFile’ = $LogFile # module EzLog ‘https://github.com/apetitjean/EZLog) the more simple a powerful logging module, i’ve found
    ‘Write-EZLog:Delimiter’ = ‘;’ # module EzLog
    ‘Write-EZLog:ToScreen’ = $true # module EzLog
    }

    $PSDefaultParameterValues[“Find-Module:Repository”] = ‘MyRepository’ # only for test
    $PSDefaultParameterValues[“Install-Module:Repository”] = ‘MyRepository’ # only for test

    Reply
  3. Oliv

    $PSDefaultParameterValues = @{
    ‘*:Encoding’ = ‘utf8’
    ‘Install-Module:Scope’ = ‘CurrentUser’ # install for me and testing without impact for everyone
    ‘Test-NetConnection:InformationLevel’ = ‘Quiet’ # no progress bar
    ‘Export-Csv:NoTypeInformation’ = $true # it will be th default
    ‘Export-Csv:delimiter’ = “;” # it’s easier to import in excel with a ; as separator (i’m french)
    ‘ConvertTo-Csv:NoTypeInformation’ = $true
    ‘Write-EZlog:LogFile’ = $LogFile # from module EZLog (https://github.com/apetitjean/EZLog) a simple and powerful logging module
    ‘Write-EZLog:Delimiter’ = ‘;’
    ‘Write-EZLog:ToScreen’ = $true
    }
    # for test only
    $PSDefaultParameterValues[“Find-Module:Repository”] = ‘MyRepository’
    $PSDefaultParameterValues[“Install-Module:Repository”] = ‘MyRepository’

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: