PowerShell - Defining default parameter values

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

Default values can be set for cmdlet parameters or advanced functions (with CmdletBinding).

It is sufficient to use the automatic variable $PSDefaultParameterValues whose value is empty by default.

You can use wildcards in the CmdletName and ParameterName values.

# Set the -Verbose parameter to all commands with the verb Get
$PSDefaultParameterValues=@{ 'Get-*:Verbose'=$true}
# Display default parameter values
$PSDefaultParameterValues
# Set the -Verbose parameter to all commands with the Get verb and -Debug to all commands
$PSDefaultParameterValues=@{ 'Get-*:Verbose'=$true;
'*:Debug'=$true }
$PSDefaultParameterValues
# Add a default value
# Add the -WhatIf parameter to the Stop-Process command
$PSDefaultParameterValues.Add('Stop-Process:WhatIf',$True)
$PSDefaultParameterValues
# Add or modify a default value
# Set the -Verbose parameter to all commands
$PSDefaultParameterValues['*:Verbose'] = $true
$PSDefaultParameterValues
# Delete a default value
$PSDefaultParameterValues.Remove('Stop-Process:WhatIf')
$PSDefaultParameterValues
# Remove all default values
$PSDefaultParameterValues.Clear()
# Disable default values
$PSDefaultParameterValues.Add('Disabled', $true)
# Alternatively
$PSDefaultParameterValues['Disabled'] = $true
# Enable default values
$PSDefaultParameterValues.Add('Disabled', $false)
# Alternatively
$PSDefaultParameterValues['Disabled'] = $false
# Source of inspiration for default values
# Provide identifiers when using the -Credential parameter
$Cred = Get-Credential
$PSDefaultParameterValues['*:Credential'] = $Cred
# Use the -Wrap parameter by default with Format-Table
$PSDefaultParameterValues['Format-Table:Wrap'] = $true
# Show hidden items with Get-ChildItem
$PSDefaultParameterValues['Get-ChildItem:Force'] = $true
# Ask Test-Connection to just ping and return True or False
$PSDefaultParameterValues['Test-Connection:Quiet'] = $true
$PSDefaultParameterValues['Test-Connection:Count'] = '1'

Related links