Adam Bertram is a 20-year veteran of IT and experienced online business professional. He’s an entrepreneur, Microsoft MVP, blogger, trainer, author and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips.

Understanding $PSDefaultParameterValues in PowerShell 7

Do you have that one PowerShell command you use constantly? If that command has parameters, you have to manually specify the parameter values every time. If you find yourself passing the same parameter values to a command frequently, this post is for you.

In this article, you’re going to learn a time-saving way to automatically provide common values to function or cmdlet parameters in PowerShell using the $PSDefaultParameterValues automatic variable.

Passing Parameter Values: The Hard Way

Let’s say you’re connecting to a lot of remote computers with the Invoke-Command cmdlet. You have configured SSL for WinRM and are repeatedly having to use the parameters: UseSSL and Port. All of your Invoke-Command references look like below. Notice that the ComputerName parameter will change but the others will not.


Now you could continue to manually type -Port 5986 -UseSSL $true for every, single Invoke-Command reference but your enemy should be the word manual.

Passing Parameter Values: The Easy Way

You need to figure out a way to automatically pass a static value to both the Port and UseSSL parameters. To do that, you have two options.

  1. You could use PowerShell 7 proxy functions. A proxy function takes the guts of a command and creates a new one. It’s essentially a wrapper for an existing cmdlet.
  2. You could use the $PSDefaultParameterValues automatic variable providing the command, parameters and their values.

Proxy functions are more complex than option #2 so let’s stick to that one for this article.

The $PSDefaultParameterValues automatic variable is a variable that contains a specifically structured hashtable. This hashtable defines command names, parameter names and their values. Before a command is invoked, PowerShell checks to see if the command exists in this variable. If so, it automatically passes the parameters provided in the variable.

Adding to $PSDefaultParameterValues

Adding to the $PSDefaultParameterValues is exactly like adding to any other hashtable with a key and value. But, the key must be structured in the format [command_name]:[parameter_name] with the value being the value to pass to the parameter.

Using the example above, let’s ensure -Port 5986 -UseSSL $true is passed to every Invoke-Command reference.


Now check the value of $PSDefaultParameterValues and see what it looks like.

Now whenever you run Invoke-Command, both of the provided parameters will be used.

Note that $PSDefaultParameterValues will only exist in the current PowerShell session. If you need to keep these settings, be sure to place the code to add the values in your PowerShell profile.

Using Wildcard Values

Perhaps you have multiple commands matching the same verb or noun to add parameter values to. Or maybe you have a single command with parameter names matching a particular pattern. In that case, you can also use wildcards when adding values.

Maybe you have some custom functions like Get-Server, Add-Server and New-Server. Each of these functions has a parameter called Type. Rather than adding a key/value pair for each of these functions, you could accomplish the same thing with a single entry.


Now any command ending with -Server will be passed a parameter of Type with a value of some_value.

Disabling $PSDefaultParameterValues

Perhaps you’ve changed your mind and would rather not have the parameters automatically passed to Invoke-Command. In that case, you could either remove the keys with $PSDefaultParameterValues.Remove() or you could disable the functionality. Disabling the functionality allows you to keep the existing values.

To disable $PSDefaultParameterValues, add a Disabled key to the hashtable with a value of $true.


All previously added key/value pairs still exist but the functionality has simply been disabled.

Summary

Whenever you find yourself using the same parameters with the same values on a command, consider using the $PSDefaultParameterValues automatic variable. It will cut down on repetitive typing and save your sanity!

Related Topics:

  • PowerShell
  • BECOME A PETRI MEMBER:

    Don't have a login but want to join the conversation? Sign up for a Petri Account

    Register