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.

Invoke-Command -Port 5986 -UseSSL $true -ComputerName [somecomputer]

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.

Sponsored Content

Passwords Haven’t Disappeared Yet

123456. Qwerty. Iloveyou. No, these are not exercises for people who are brand new to typing. Shockingly, they are among the most common passwords that end users choose in 2021. Research has found that the average business user must manually type out, or copy/paste, the credentials to 154 websites per month. We repeatedly got one question that surprised us: “Why would I ever trust a third party with control of my network?

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.

$PSDefaultParameterValues.Add("Invoke-Command:Port", 5986)
$PSDefaultParameterValues.Add("Invoke-Command:UseSSL", $true)

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.

$PSDefaultParameterValues['Disabled'] = $true

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


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:


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

Comments (1)

One response to “Understanding $PSDefaultParameterValues in PowerShell 7”

  1. <p>Oh man, this works on Powershell 5 too! This is a game changer for some of my SCSM query scripts. No more injecting the target server as a variable to every single get/set scsm cmdlet invoked forever and unto eternity. Because I always forget the first time, and then wait for the timeout and impending error to remind me…………</p><p><br></p><p>Thanks for this, on the toolbelt it goes!</p>

Leave a Reply

Entrepreneur, hustler, husband, dad, Automator, content producer, published author, Microsoft MVP, DevOps pro and passionate problem-solver.