Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to define a default value for a parameter in a PowerShell command.
Add an entry to the PSDefaultParameterValues
hashtable:
PS > Get-Process Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 150 13 9692 9612 39 21.43 996 audiodg 1013 84 45572 42716 315 1.67 4596 WWAHost (...) PS > $PSDefaultParameterValues["Get-Process:ID"] = $pid PS > Get-Process Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 584 62 132776 157940 985 13.15 9104 powershell PS > Get-Process -Id 0 Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 0 0 0 20 0 0 Idle
In PowerShell, many commands (cmdlets and advanced functions) have parameters that let you configure their behavior. For a full description of how to provide input to commands, see “Running Commands”. Sometimes, though, supplying values for those parameters at each invocation becomes awkward or repetitive.
In early versions of PowerShell, it was the responsibility of each cmdlet author to recognize awkward or repetitive configuration properties and build support for “preference variables” into the cmdlet itself. For example, the Send-MailMessage
cmdlet looks for the $PSEmailServer
variable if you do not supply a value for its
-SmtpServer
parameter.
To make this support more consistent and configurable, PowerShell supports the PSDefaultParameterValues
preference variable. This preference variable is a hashtable. Like all other PowerShell hashtables, entries come in two parts: the key and the value.
Keys in the PSDefaultParameterValues
hashtable must match the pattern cmdlet
:parameter
—that is, a cmdlet name and parameter name, separated by a colon. Either (or both) may use wildcards, and spaces between the command name, colon, and parameter are ignored.
Values for the cmdlet/parameter pairs can be either a simple parameter value (a string, boolean value, integer, etc.) or a script block. Simple parameter values are what you will use most often.
If you need the default value to dynamically change based on what parameter values are provided so far, you can use a script block as the default. When you do so, PowerShell evaluates the script block and uses its result as the default value. If your script block doesn’t return a result, PowerShell doesn’t apply a default value.
When PowerShell invokes your script block, $args[0]
contains information about any parameters bound so far: BoundDefaultParameters
, BoundParameters
, and BoundPositionalParameters
. As one example of this, consider providing default values to the -Credential
parameter based on the computer being connected to. Here’s a function that simply outputs the credential being used:
function
RemoteConnector
{
param
(
[
Parameter
()]
$ComputerName
,
[
Parameter
(
Mandatory
=
$true
)]
$Credential
)
"Connecting as "
+
$Credential
.
UserName
}
Now, you can define a credential map:
PS > $credmap = @{} PS > $credmap["RemoteComputer1"] = Get-Credential PS > $credmap["RemoteComputer2"] = Get-Credential
Then, create a parameter default for all Credential
parameters that looks at the
ComputerName
bound parameter:
$PSDefaultParameterValues
[
"*:Credential"
]
=
{
if
(
$args
[
0
].
BoundParameters
-contains
"ComputerName"
)
{
$cred
=
$credmap
[
$PSBoundParameters
[
"ComputerName"
]]
if
(
$cred
)
{
$cred
}
}
}
Here is an example of this in use:
PS > RemoteConnector -ComputerName RemoteComputer1 Connecting as UserForRemoteComputer1 PS > RemoteConnector -ComputerName RemoteComputer2 Connecting as UserForRemoteComputer2 PS > RemoteConnector -ComputerName RemoteComputer3 cmdlet RemoteConnector at command pipeline position 1 Supply values for the following parameters: Credential: (...)
For more information about working with hashtables in PowerShell, see “Hashtables (Associative Arrays)”.
“Hashtables (Associative Arrays)”
“Running Commands”