Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET!

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

Read the Best Personal and Business Tech without Ads

Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.

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. AlanM

    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............

    Thanks for this, on the toolbelt it goes!

Leave a Reply

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

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.


Sponsored By