You may have heard about PowerShell default parameter values and $PSDefaultParameterValues. When you assign a hash table to this special variable, the key defines the commands and parameters affected, and the value defines your new default value.

Take a look at this example:

$PSDefaultParameterValues = @{
    '*:ComputerName' = 'testserver1'

This would set the -ComputerName parameter for all commands (“*”) to the new default value “testserver1”. Whenever you call a command that (a) has a parameter named “ComputerName” and (b) you did not explicitly assign a value to this parameter, your default value will be used.

This also works for PowerShell functions, however only for “Advanced Functions”. “Simple Functions” do not qualify.

Check yourself, and define $PSDefaultParameterValues like above. Next, run this code:

function testSimple
    "Result Simple: $Computername" 

function testAdvanced
    "Result Advanced: $Computername" 



As you will see, only testAdvanced picks up the default parameter. “Advanced Functions” are defined by using at least one parameter attribute, such as [CmdletBinding()] or [Parameter(Mandatory)].

Twitter This Tip! ReTweet this Tip!