Defining Default Parameters

by Sep 8, 2015

PowerShell can define defaults for any parameter, so if you wanted to always submit a default value for Get-ChildItem‘s –Path parameter, this is what you can do:

PS> $PSDefaultParameterValues['Get-ChildItem:Path'] = 'C:\$Recycle.Bin'

When you then run Get-ChildItem (or one of its aliases like dir), and not submit the –Path parameter, then PowerShell would always use the value specified in $PSDefaultParameterValues variable.

You can use wildcards, too. For example, if you want to set the default for the –Server parameter in all of your AD cmdlets, try this:

PS> $PSDefaultParameterValues['*-AD*:Server'] = 'dc-01'

$PSDefaultParameterValues is actually a hash table, so you can overwrite default parameters, or dump the list of all currently defined defaults:

Name                           Value                                                                 
----                           -----                                                                 
*-AD*:Server                   dc-01                                                                 
Get-ChildItem:Path             C:\$Recycle.Bin

To clear all default parameters, clear the hash table:

PS> $PSDefaultParameterValues.Clear()

Twitter This Tip! ReTweet this Tip!