PowerShell Version 3 introduces the concept of Default Parameter Values.

This allows you to specify a value for one or more parameters for one or more commands.

To implement this, you need to populate a new hashtable – $PSDefaultParameterValues

That Description is Not Helpful

Let’s look at some examples -

First off, we can specify the value for a parameter for a specific command.  The pattern used for the hashtable entries is “NameOfCommand:NameOfParameter” = “ValueOfParameter”.

(I’m stealing some of these examples from the samples provided in CTP1.)

$PSDefaultParameterValues = @{"Get-Process:Name"="powershell"}

Get-Process

Get-Process e*

With that set, every time we run Get-Process and do not specify the Name parameter, the PowerShell runtime will stick “powershell” in for that value.  A specified parameter will always override a default value.



Let’s go the other way and, instead of specifying one command, set a parameter value on all commands (that have that parameter.

$cred = Get-Credential mydomain\mydomainadminaccount

$PSDefaultParameterValues = @{"*:Credential" = $cred}
This would provide your $cred to any Credential parameter.



We can also use wildcards to match multiple commands.
$PSDefaultParameterValues = @{"*-AD*:Server" = 'MyDomainController'}
This would provide a default value to the Server parameter for any command whose noun started with AD (like everything in the ActiveDirectory module.



Since this is a hashtable, it can hold many different mappings - 

$PSDefaultParameterValues = @{

  "*:Credential" = $cred

  "Get-Process:Name"="powershell"

  "*-AD*:Server" = 'MyDomainController'

}
To turn off DefaultParameterValues (without removing everything you have set up), you can set a key “Disabled” equal to $true.

$PSDefaultParameterValues += @{"Disabled" = $true}

This concludes our brief look at DefaultParameterValues for today.

Go grab Version 3 CTP2 and give it a whirl…

Want more great reading? Check out my reading list!