Working with PowerShell default parameters to change their behavior

If you have read some of my other articles on this site, then you have probably noticed that I tend to do a lot with PowerShell. Personally, I like working with PowerShell because it allows you to do things that you just can’t do with the Windows GUI. Even so, there is no denying that some of the PowerShell cmdlets can be a bit tedious, or dare I say complicated, to use. But what if there was a way to bend these cmdlets to your will and make them behave the way that you want them to? Believe it or not, there is a relatively easy way to do just that. You can change a cmdlet’s behavior by modifying the values of the PowerShell default parameters. In this article, I will show you how it’s done.

Word of caution

Before I get started, I need to give you a quick word of caution. Even though it is super-convenient to be able to change a cmdlet’s PowerShell default parameters and its behavior, there can be consequences to doing so. If you run a script, and that script assumes that a modified cmdlet will behave in a certain way, you could end up with unpredictable results. Therefore, you should be sure to modify PowerShell default parameters cautiously.

Quick review

Now that I have gotten the warning out of the way, there are two PowerShell concepts that you will need to be familiar with in order to work with default parameters. The first of these concepts is that of a variable. In PowerShell, variable names always start with a dollar sign, and you can view the contents of a variable by entering the variable name.

The other concept that you will need to be familiar with is that of a hash table. A hash table is basically just a list made up of key/value pairs. To give you a quick example, suppose that we wanted to create a hash table containing the names of U.S. states and their abbreviations. Here is what the code might look like:


$StateList = @{}
$StateList.add(‘Florida’,’FL’)
$StateList.add(‘South Carolina’,’SC’)
$StateList.add(‘Georgia’,’GA’)
$StateList


The first line of code creates an empty hash table called $StateList. The next three lines add entries to the table. Each entry is made up of a key (the state name) and a value (the state abbreviation). The last line of code lists the table’s contents. You can see the code in action in the figure below.

PowerShell default parameters
Keep in mind that this is a very simple example and that there are lots of ways to use a hash table. For right now, though, I just want to introduce you to the basic concept.

Working with PowerShell default parameters

PowerShell has a built-in variable that it can use to store the default parameters to be used with its cmdlets. That variable is called $PSDefaultParameterValues. As you have probably already figured out from both my introduction and the variable’s name, this is no ordinary variable, but rather a hash table. If you enter the variable’s name into PowerShell, however, you will quickly see that the table is empty, as shown below.

PowerShell default parameters
OK, so what can we actually do with the $PSDefaultParameterValues variable? As previously mentioned, you can control the default behavior of any PowerShell cmdlet. The only catch is that you can’t go beyond a cmdlet’s existing capabilities. So let’s take a look at an example.

This example probably isn’t the sort of thing that you would want to do in real life, but I am using it because it clearly shows just how radically you can change a cmdlet’s behavior.

If you enter the Get-TimeZone cmdlet into PowerShell, you will see the name of the time zone that the PC is currently configured to use. You can see an example of this in the figure below.

PowerShell default parameters
So what if we wanted to change this cmdlet so that it does not show which time zone we are in, but rather which time zones are available for use? To do so, we need to get the cmdlet’s syntax. That can be accomplished by using the Get-Help cmdlet followed by the Get-TimeZone cmdlet. You can see the cmdlet’s syntax below.

PowerShell default parameters
The syntax includes a parameter called ListAvailable. So with that in mind, let’s enter this command:

$PSDefaultParameterValues.Add(“Get-TimeZone:ListAvailable”,$True)

The first part of this command simply tells PowerShell that we want to add a value to a hash table, just like we did with the state abbreviations example earlier in this article. Like the earlier example, the hash table contains a key/value pair. The key, in this case, is the cmdlet name (Get-TimeZone) followed by a colon and the name of the parameter that we want to set. In this case, the parameter name is ListAvailable. The second part of the cmdlet is the value that we want to assign to the parameter. In this case, I am using $True. The ListAvailable parameter doesn’t normally need a value, so specifying $True is how we tell PowerShell to use the parameter without us having to assign a value to it.

If you look at the figure below, you can see the command that I entered. More importantly, you can see what now happens when I run the Get-TimeZone cmdlet.

PowerShell default parameters

Things to remember

The most important thing to understand about adding new values to PowerShell default parameters is that they only change a cmdlet’s default behavior, they do not remove capabilities from the cmdlet. Even though I changed the Get-TimeZone cmdlet so that it shows available time zones instead of my current time zone, I could still display information about my own time zone if I wanted to jump through a few hoops.

Another thing that you need to know is that your custom default parameters can be removed. To remove a single item, use the $PSDefaultParameterValues.Remove command, followed by the name of the item that you want to remove. For example, you might enter:

$PSDefaultParameterValues.Remove(“Get-TimeZone:ListAvailable”)

As an alternative, you can clear the entire contents of the hash table by using this command:

$PSDefaultParameterValues.Clear();

You can see examples of both techniques below:

PowerShell default parameters
You can clear an entire hash table or remove a single item.

Changing the default behavior of a PowerShell cmdlet is not something you will do often, but when you need to, now you know how.

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top