Windows PowerShell Default Parameters

PowerShell 3.0 $PSDefaultParameterValues

New in PowerShell 3.0 is the ability to control the default value of parameters such as Logname.  This technique, using a built-in variable called $PSDefaultParameterValues will save you typing if you spend a lot of time with a cmdlets such as Get-Eventlog.

PowerShell Default Parameter Topics

 ♦

Refresher: List PowerShell’s Built-In Variables

I would like to begin with an explanation of how I discovered this and other built-in PowerShell variables.

# List PowerShell’s Automatic, Built-In Variables
Get-Variable PS*

Note 1: Remove the PS* to see all PowerShell’s variables.

Changing PowerShell’s Default Parameters

The plus of changing a parameter’s default is that you can control factors such as which eventlog responds to the plain Get-Eventlog command. But remember they only last for that session, therefore you may like to load them with your PowerShell profile file.

Preliminary ExperimentPowerShell 3.0 $PSDefaultParameterValues  Default Parameters Variable
If you run plain Get-Eventlog, without any parameters, then you get a box prompting you for the name of a log.  However, the $PSDefaultParameterValues variable can set the default value for the LogName parameter to ‘Application’, or any of the other logs.

$PSDefaultParameterValues."Get-Eventlog:logname"="Application"

Result
Once you have run the above command, then Get-Eventlog will know that you want the application log, and therefore there is no need for a dialog box to prompt you for the log’s name.

Note 2: Absorb the rhythm of the $PSDefaultParameterValues variable.  Start with full stop!  Observe the double quotes around cmdlet and its parameter, and the full colon separating cmdlet and parameter.  If the value is a text string then that too needs speech marks.

Remember that the point is to change the default behaviour of the cmdlet: now type just Get-Eventlog and this is what you should see.PowerShelll Default Parameters Variable

Note : If you want the system log just type Get-Eventlog System, and it will over-ride the default value of ‘Application’.

Alternative .Add Method

$PSDefaultParameterValues.Add("Get-Process:Name","svchost")

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

Start Again! – Clear Your Custom Parameter Values

Here is a command to reset all the defaults that you have created.  I recommend that you memorize it early in your career of experimenting with PowerShell’s parameters.

# Delete all modifications to parameter defaults
$PSDefaultParameterValues.clear()

Note 3: Before clearing the values I like to run $PSDefaultParameterValues.keys.  Incidentally, there is also a .values method.

A Better Method – Temporarily Disable Custom Values

The beauty of this command is that it stores all your custom PowerShell default parameters, and merely turns them off.  By changing $true to $false you can reactivate all your carefully created default parameters.

# Keep your custom parameters, but turn them off
$Global:PSDefaultParameterValues["Disabled"]=$true
# $Global:PSDefaultParameterValues["Disabled"]=$false

Note 4: Naturally, you need to remove the # if you want to reverse the command and thus enable your custom parameters once more.

How To Research More Parameters

Whichever cmdlet you are experimenting with, you can research additional parameter with Get-Help.

Get-Help Get-Eventlog -full

Note 5: Help reveals useful parameters such as computer

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Advanced Technique Using an @ {Array}

Here is a method to set multiple parameter values with an array.

$PSDefaultParameterValues=@{
"Get-Eventlog:newest"=50;
"Get-Eventlog:logname"="Application";
}

A pleasant surprise – at least to me!

Using the variable $PSDefaultParameterValues you can mix and match parameters from different cmdlets in your array.  This makes using arrays really worthwhile as you can amend your master list, rerun the command and thus refresh your parameters’ defaults.  For perfection, I store my array in my PowerShell startup profile.

$PSDefaultParameterValues=@{
"Get-Process:Name"="power*";
"Get-Eventlog:newest"=50;
"Get-Eventlog:logname"="Application";
"Get-Service:Name"="M*";
}

Note 6: As you add more default parameters remember the syntax; in particular, the semi-colon at the end of each line.  I also discovered that numeric values don’t need speech marks.

One more surprise: You can employ wildcards* in the values of name parameters.

See more on PowerShell splatting to format your parameters.

More Examples of Setting PowerShell Default Parameters

Do remember that these changes are personal!  The downside of taking these short-cuts is that if your scripts rely on the new defaults, then colleagues will get different results if they run the same script on other machines with their factory defaults.

# GWMI setting both class and compound filter.
$PSDefaultParameterValues=@{
"Get-WmiObject:class" = "Win32_NTLogEvent";
"Get-WmiObject:filter" ="(logfile=’Application’) AND (type=’error’)" ;
}

Setting The Default Name Parameter

The point of this example is that you need to explicitly script the name parameter, even though PowerShell normally assumes it’s in the first position.  Perhaps I was naive, but I thought this would work:
"Restart-Service" ="Spooler" ;  Wrong!  I needed to insert :name, see below.

$PSDefaultParameterValues=@{
"Restart-Service:name" ="Spooler" ;
"Get-ChildItem:path " ="C:\Windows\System32" ;
"Get-Date:uformat" = "%A, %d:%m:%Y" ;
}

Note 7: Variation showing how to set a default value for gci’s -path parameter.

Note 8: The last example shows how to set date format for UK readers, we prefer d=day then m=month.

See Array strings in PowerShell »

Summary of PowerShell v 3.0 Default Parameters

In PowerShell 3.0 we can control the default values of parameters with an automatic variable called: $PSDefaultParameterValues.  This technique will save typing if you spend a lot of time with a cmdlet.

The plus of changing a parameter’s default is that you can control factors such as which eventlog responds to the plain Get-Eventlog command.

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.