Prof. Powershell

Default Parameter Values: Better with PS 3

Working with variables will improve with the next version of PowerShell. Take this week's example, $PSDefaultParameterValues, a new automatic variable.

In PowerShell 2.0, we can use variables for commonly used parameter values:

PS C:\> $class="win32_operatingsystem"
PS C:\> get-wmiobject -class $class

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 7601
RegisteredUser  : LocalAdmin
SerialNumber    : 00426-065-0389393-86517
Version         : 6.1.7601

If we change the value of $class, we can re-run the Get-WMIObject command without editing. This helps a little bit towards efficiency, but PowerShell 3.0 will take this to a new level. In version 3 we will be able to define a set of default parameter values. If we run a command that has one of the defined defaults, PowerShell will automatically use it. Or we can specify a different value.

This is accomplished with a new automatic variable, $PSDefaultParameterValues. This value is a specially constructed hash table. I'm going to set a default value that is similar to my $class example above.

PS C:\> $PSDefaultParameterValues.Add("Get-WmiObject:class","Win32_OperatingSystem")

The key is the cmdlet and parameter name, separated by a colon. But now when I run Get-WMIObject, it will automatically use this value:

PS C:\> Get-WmiObject

SystemDirectory : C:\Windows\system32
Organization    :
BuildNumber     : 7601
RegisteredUser  : LocalAdmin
SerialNumber    : 00426-065-0389393-86517
Version         : 6.1.7601

Or I can override the default:

PS C:\> Get-WmiObject win32_pagefilesetting

      MaximumSize Name                Caption
      ----------- ----                -------
                0 d:\pagefile.sys d:\ 'pagefile.sys'

I can have as many entries as I'd like. But remember, this is just another variable so any changes you make to it will disappear when you exit your session. If you have some values you'd always like to use, you can define this variable in your profile:

$PSDefaultParameterValues=@{
"get-eventlog:logname"="system";
"get-eventlog:newest"=5;
"Enter-PSSession:computername"="coredc01"
}

These default values can simplify my command-line typing:

PS C:\> get-eventlog

Index Time         EntryType   Source               InstanceID Message
----- ----         ---------   ------               ---------- -------
13782 Feb 02 11:12 Information Service Control M... 1073748860 The Win...
13781 Feb 02 11:12 Information Service Control M... 1073748864 The sta...
13780 Feb 02 11:12 Information Service Control M... 1073748864 The sta...
13779 Feb 02 11:01 Information Service Control M... 1073748860 The Win...
13778 Feb 02 11:01 Information Service Control M... 1073748860 The Win...

PS C:\> get-eventlog application

Index Time         EntryType   Source               InstanceID Message
----- ----         ---------   ------               ---------- -------
55169 Feb 01 16:41 0           CertEnroll           2186936384 The des...
55168 Feb 01 16:41 0           CertEnroll           2186936385 The des...
55167 Feb 01 09:59 Information SceCli               1073743528 Securit...
55166 Feb 01 08:58 0           Microsoft-Windows... 258        The dis...
55165 Feb 01 08:58 Error       SideBySide           3238068296 Activat...

PS C:\> enter-pssession
[coredc01]: PS C:\Users\Administrator\Documents>

Because this is a hash table I can modify values, although I have to account for the funky property name:

PS C:\> $PSDefaultParameterValues."get-eventlog:newest"=10

I can remove a default.

PS C:\> $PSDefaultParameterValues.Remove("get-eventlog:newest")

Or I can clear the hash table altogether:

PS C:\> $PSDefaultParameterValues.Clear()

I have to say I love this feature and am looking forward to taking advantage of it.

Note: This information is based on PowerShell v3.0 CTP 2 which is subject to change, and most likely will, prior to release.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025