Because PowerShell is a console based management tool there’s no getting around the fact that there is a fair bit of typing. Fortunately, PowerShell offers a number of features to minimize typing such as aliases and the use of positional parameters. In PowerShell 3.0 we now have another feature, default parameter values.

Don’t confuse this with cmdlet default values which we’ve always had. Some cmdlets, like Get-ChildItem, that have mandatory parameters are defined with a default value. For example, you need to specify a path when using Get-ChildItem, or its alias dir. But if you don’t specify a path, the cmdlet defaults to the local directory.

In PowerShell 3.0 we can take this a step further. Suppose when you use Get-EventLog you almost always are querying the System event log. Now you can set a default value for that cmdlet so that whenever you run Get-Eventlog it will use that value. If you want to query a different eventlog simply use the parameter as you normally would and specify a different value.

Defining default oarameter values ^

The default parameters are stored in a variable called $PSDefaultParameterValues. The name is reserved but the variable is undefined. And because it is a variable, it must be defined in every session where you intend to use it. If you are going to use this feature daily, I recommend defining this variable in your PowerShell profile.

I don’t recommend using $PSDefaultParameterValues in your scripts. This adds an unnecessary dependency and makes your scripts less portable unless you include the variable definition. I think it goes against the scripting best practice of using full cmdlet and parameter names. Remember, you only have to type once in a script so this feature doesn’t really help much. But from an interactive prompt, especially where typing is awkward such as on a mobile device using PowerShell Web Access, this is a life saver.

The $PSDefaultParameterValues variable is actually an associative array, or hash table. The key is the cmdlet name followed by a colon and then the parameter name. The value is whatever you want to assign as the default value. For switch parameters like –Recurse, set the value to $True. Here’s an example.

PS C:\> $PSDefaultParameterValues=@{"Get-Eventlog:Logname"="System";"Get-Wmiobject:classname"="Win32_Operatingsystem"}

I’ve defined a default value for –Logname from Get-Eventlog and one for –Classname from Get-WmiObject. PowerShell won’t verify if you typed in valid cmdlet and parameter names. But as you can see in the screenshot PowerShell automatically inserts these values into my commands.

PowerShell 3.0 Default Parameter Values

PowerShell 3.0 Default Parameter Values

You can define as many values as you want when initially defining the array. Just don’t forget to put the cmdlet and parameter names in quotes. If you want to see what is in the variable, simply ask.

PS C:\> $PSDefaultParameterValues
Name Value
---- -----
Get-Wmiobject:classname Win32_Operatingsystem
Get-Eventlog:Logname System

Adding a parameter ^

Once the variable has been defined you can add more default values as you would any other hash table.

PS C:\> $PSDefaultParameterValues.Add("Get-CimInstance:Classname","Win32_OperatingSystem")

As you can see in the screenshot below it just works.

Adding a Default Parameter

Adding a Default Parameter

Modifying a parameter value ^

You change a value in the hash table like any other hash table. The tricky part is remembering that the key is the cmdlet:parameter string. I like displaying the value first so not only can I verify my syntax but also double-check what I’m about to do.

PS C:\> $PSDefaultParameterValues."Get-Eventlog:logname"
PS C:\> $PSDefaultParameterValues."Get-Eventlog:logname"="Application"
PS C:\> $PSDefaultParameterValues."Get-Eventlog:logname"

Now when I run Get-Eventlog, PowerShell will default to the Application log.

Removing a parameter ^

Remember, you can override a default value by specifying a new one.

PS C:\> gwmi win32_bios

But you may decide during the course of session that you need to remove an entry. Use the hash table’s Remove() method:

PS C:\> $PSDefaultParameterValues.Remove("get-eventlog:logname")

To wipe out the entire array you can use the Clear() method.

PS C:\> $PSDefaultParameterValues.Clear()

Or you can define an entirely new array of default values.

Summary ^

This feature will work for cmdlets or workflows, and user-defined advanced functions, i.e. those using cmdlet binding. Using default parameter values can save a lot of typing and make you even more efficient from a PowerShell prompt. Review your typical commands. If you find yourself repeatedly defining a parameter that would be a good candidate for $PSDefaultParameterValues.


Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account