Using Default Parameters

by Jul 4, 2014

In PowerShell 3.0, an option was added to define default values for arbitrary cmdlet parameters.

This line, for example, would set the default value for the parameter -Path of all cmdlets to a given path:

$PSDefaultParameterValues.Add('*:Path', 'c:\Windows')

So when you now run Get-ChildItem or any other cmdlet that has a parameter -Path, it behaves as if you had specified the given path for this parameter.

Instead of the "*", you can of course add the name of a specific cmdlet. So if you wanted to set the parameter -ComputerName for Get-WmiObject to a specific remote system, this is how you would do that:

$PSDefaultParameterValues.Add('Get-WmiObject:ComputerName', 'server12')

All of these defaults are valid only in the current PowerShell session. If you want to keep them, then simply define the default values in one of your profile scripts.

To remove all custom default values again, use this:

$PSDefaultParameterValues.Clear()

Twitter This Tip! ReTweet this Tip!