default parameter values

When connecting to a SQL Server instance with alternative credentials, it can be tedious to repeat the SQL credential over and over.

The great news is that this repetition is not required, as it can be handled instead by $PSDefaultParameterValues

Intro to $PSDefaultParameterValues

$PSDefaultParameterValues is a hashtable available in PowerShell that can set defaults for any command that you run. In it’s simplest form, setting a default parameter value can look like this:

According to Microsoft, $PSDefaultParameterValues:

  • Let you specify custom default values for any command
  • Commands use the custom default value unless you specify another value
  • Useful when you must specify the same value nearly every time

After running the above code, Get-DbaDatabase will show verbose output every time it’s executed, without me having to specify -Verbose. If I need to override that verbose flag for some reason, I can simply add -Verbose:$false to my Get-DbaDatabase command.

image

Usage

Here’s what I’ve used $PSDefaultParameterValues for:

    •  SqlCredential for SQL Authentication in docker
    •  ErrorAction Stop in Agent Jobs to ensure failures fail
    •  Confirm:$false in Agent Jobs to avoid prompting
    •  Verbose in Agent Jobs to see command output in the logs
    •  EnableException in dbachecks to ensure Pester tests fail
    •  SqlCredential for any alternative credential needs, actually
    •  Invoke-WebRequest Proxy and ProxyUseDefaultCredentials

You could even set a default SQL Server and get results from Get-DbaDatabase.

image

If you have configured a good amount of default parameters and want to see all of your default parameter values, you just need to run $PSDefaultParameterValues .

image

Oh, and it’s no big deal if a command does not support the parameter, the parameter and its value just won’t be passed. And also, like most things that support defaults, you can override the defaults, as mentioned previously.

Syntax & real-world examples

Michael Sorens awesome article on simple-talk goes in-depth about syntax, but here are some basics.

In the real-world, I primarily use Default Parameter Values when running Scheduled Tasks/Agent Jobs. I like to see verbose output in my logs, so I enable that. I also make sure there are no confirmation prompts; that got me once as I waited over 24 hours for a job to complete (“daaang this code is slow”) 😅

Disabling $PSDefaultParameterValues

Your default parameter values can be temporarily disabled by adding the following key:

And you can reenable it by setting Disabled to $false or by removing the Disabled key.

Persistence

Setting $PSDefaultParameterValues at the command-line only lasts during that session, meaning if you close your console, it will be reset. So, if you want it the default values to persist, you’ll have to add it to your profile (basically: notepad $profile, paste, save).

Scopes are dope

In dbachecks, we set $PSDefaultParameterValues to EnableException across the board. This allows our Pester tests to fail when a failure occurs.

What’s interesting is that setting the value within the module does not impact the end-user, so even after you import dbachecks, your $PSDefaultParameterValues will not be modified.

image

Resources

There are some great resources that go more in-depth about Parameters Default Values. Here are just a few:

- Chrissy

One thought on “default parameter values

  1. Pingback: Default Parameter Values In Powershell – Curated SQL

Leave a Reply

Your email address will not be published. Required fields are marked *

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