PowerShell $PSDefaultParameterValues

Change the Default Parameter Values in PowerShell v 3.0

It is possible to modify the default action of a PowerShell cmdlet by modifying its parameters.  For example, it annoys me that Get-EventLog does not focus on the Application log.  Thanks to $PSDefaultParameterValues we can launch a Cmdlet in the way that we want.

$PSDefaultParameterValues Topics


Change the Behaviour of Get-Eventlog with $PSDefaultParameterValues

Normally, if you run the cmdlet Get-Eventlog it prompts you with:

However, in PowerShell version 3.0 you can change what happens when you execute a cmdlet by using an interesting built-in function called: $PSDefaultParameterValues.

# Change Default Values of a Cmdlet.
# N.B. Requires PowerShell 3.0

Note 1:  In the present PowerShell session, whenever you type Get-Eventlog on its own, it returns the 50 lastest entries in the application log.

How to Reset $PSDefaultParameterValues

The quickest way to nullify $PSDefaultParameterValues is to close, then reopen PowerShell.  A better way to over-ride the command temporarily is thus:

$PSDefaultParameterValues.Add("Disabled", $true)

Alternatively, you could remove values from the hash table.

$PSDefaultParameterValues.Add("Disabled", $False) $PSDefaultParameterValues.Remove("Get-Eventlog:newest")

Note 2: If you now try Get-Eventlog the results should reveal the Application events, but it won’t halt after the 50th because we removed the :newest parameter.

Tip: Once you have identified suitable jobs for $PSDefaultParameterValues, place them in your profile.ps1 file.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Research Methods for $PSDefaultParameterValues

We have already used the .add method, now let us see what other methods and properties are available:

# Research $PSDefaultParameterValues
$PSDefaultParameterValues | Get Member

Useful methods include ‘Clear’, and property ‘Values’.

Identify Cmdlets with Suitable Parameters

These are the parameter characteristics that I look for when I want to change their default actions:

Required? True
Position? 1
Default value [blank, nothing set]

Here is a work-In-progress script to help with our research.

# PowerShell Parameters to change default action
$Source = Get-Command Get*
Foreach ($cmdlet in $Source) {
$Cmdlet.ParameterSets | Select-Object -ExpandProperty parameters | `
Where {$_.Position -gt "-1" -And $_.isMandatory -eq $true } `
| FT $Cmdlet.Name, Name, Position, IsMandatory -auto

Note 3: This relies on Select-Object -ExpandProperty parameters.

Note 4: I have been experiment with different values for the ‘Where’ clause.

See more on setting default PowerShell parameters »

Summary of $PSDefaultParameterValues

You can improve productivity by changing the default behaviour of a PowerShell cmdlet.  For example, you can set the action of the plain command: Get-EventLog so that it starts outputting the contents of the Application log.  The secret is to master PowerShell’s $PSDefaultParameterValues.

If you like this page then please share it with your friends


See more example of PowerShell’s parameters

PowerShell Tutorials  • PowerShell Parameter Introduction  • Parameter Examples of Technique

Mandatory Parameters  • PowerShell $PSDefaultParameterValues  • Top 10 PowerShell Parameters

PowerShell Parameters Index  • Parameter Hashtable Splatting  • PowerShell 3.0 Default Parameters

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.