Ironman Software Forums
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Default parameter values allow you to assign static and dynamic default values to cmdlet parameters within a PowerShell session. This post will take you through some examples of using the $PSDefaultParameterValues
hashtable.
The $PSDefaultParameterValues
hashtable can be defined to set static parameter values. The key in the hashtable is the name of the cmdlet and parameter you wish to set followed by the value.
$PSDefaultParameterValues = @{
"Write-Host:Object" = "Hello, World!"
}
Write-Host
In addition to setting a static value, you can also assign a script block that will be invoked to set the default value.
$PSDefaultParameterValues = @{
"Write-Host:Object" = "Hello from $($PSversionTable.OS)!"
}
Write-Host
You can adjust default parameter values at runtime by using standard hashtable features such as indices and the Add
and Remove
methods.
$PSDefaultParameterValues = @{
"Write-Host:Object" = "Hello, world!"
}
$PSDefaultParameterValues["Write-Host:Object"] = "Hello, Universe!"
Write-Host
$PSDefaultParameterValues.Add("Write-Host:ForegroundColor", "Red")
Write-Host
$PSDefaultParameterValues.Remove("Write-Host:ForegroundColor")
Write-Host
To disable default parameter values, add a Disabled
key and set it to $true
.
$PSDefaultParameterValues = @{
"Write-Host:Object" = "Hello, world!"
}
$PSDefaultParameterValues.Add("Disabled", $true)
Write-Host
Find this useful? Please consider sharing this article. Have a question about PowerShell? Contact us and we'll write a post about it.
Continue the conversion on the Ironman Software forums. Chat with over 1000 users about PowerShell, PowerShell Universal, and PowerShell Pro Tools.
Receive once-a-month updates about Ironman Software. You'll learn about our product updates and blogs related to PowerShell.