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:
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.
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
UseSSL parameters. To do that, you have two options.
- 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.
- You could use the
$PSDefaultParameterValuesautomatic 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.
$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 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
$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.
$PSDefaultParameterValueswill 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
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
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.
$PSDefaultParameterValues, add a
Disabled key to the hashtable with a value of
$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!