Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

PowerShell Default Parameters in v 3.0

If you are not aware, a developer version of Windows 8 was released at the build conference.  For the purpose of this blog I will use a specific tag (3.0 Win 8 Developer Build) for these posts so that I can remove and clean them up once PowerShell 3.0 is RTM.

I was looking at the new variables with dir variable: in PowerShell 3.0 and one caught my eye: $PSDefaultParameterValues

Interesting.  The first thing to do is inspect it.  The following shows the trail of that inspection:

PS C:\Users\toenuff.Win8> $PSDefaultParameterValues

_____________________________________________________________________________________________________
PS C:\Users\toenuff.Win8> $PSDefaultParameterValues.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    DefaultParameterDictionary               System.Collections.Hashtable                 

_____________________________________________________________________________________________________
PS C:\Users\toenuff.Win8> $PSDefaultParameterValues |gm

   TypeName: System.Management.Automation.DefaultParameterDictionary

Name                 MemberType            Definition
----                 ----------            ----------
Add                  Method                System.Void Add(System.Object key, System.Object value)
ChangeSinceLastCheck Method                bool ChangeSinceLastCheck()
Clear                Method                System.Void Clear()
Clone                Method                System.Object Clone()
Contains             Method                bool Contains(System.Object key)
ContainsKey          Method                bool ContainsKey(System.Object key)
ContainsValue        Method                bool ContainsValue(System.Object value)
CopyTo               Method                System.Void CopyTo(array array, int arrayIndex), System.Vo...
Equals               Method                bool Equals(System.Object obj)
GetEnumerator        Method                System.Collections.IDictionaryEnumerator GetEnumerator(), ...
GetHashCode          Method                int GetHashCode()
GetObjectData        Method                System.Void GetObjectData(System.Runtime.Serialization.Ser...
GetType              Method                type GetType()
OnDeserialization    Method                System.Void OnDeserialization(System.Object sender)
Remove               Method                System.Void Remove(System.Object key)
ToString             Method                string ToString()
Item                 ParameterizedProperty System.Object Item(System.Object key) {get;set;}
Count                Property              System.Int32 Count {get;}
IsFixedSize          Property              System.Boolean IsFixedSize {get;}
IsReadOnly           Property              System.Boolean IsReadOnly {get;}
IsSynchronized       Property              System.Boolean IsSynchronized {get;}
Keys                 Property              System.Collections.ICollection Keys {get;}
SyncRoot             Property              System.Object SyncRoot {get;}
Values               Property              System.Collections.ICollection Values {get;}

I’m not sure what DefaultParameterDictionary is, but it’s derived from a hash table. So, what happens when you try to set some values in there?

PS C:\Users\toenuff.Win8> $PSDefaultParameterValues.tome = 'blah'
The key 'tome' has an invalid format. The key must consist of one command name and one parameter name
separated only by a colon (CommandName:ParameterName). The command name and/or the parameter name can
be enclosed in individual quotation marks ("CommandName":"ParameterName"), but the entire key cannot be
enclosed in quotation marks. To disable the $PSDefaultParameterValues feature, set the key/value pair
to Disabled=$true.
At line:1 char:1
+ $PSDefaultParameterValues.tome = 'blah'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidOperation

JACKPOT!!!! We learn that the key name needs to be command:parametername and we learn that we can disable this feature by setting $PSDefaultParameterName.Disabled = $true.

It looks like we can set default parameter values now. Just choose the cmdlet along with parameter name as follows:

$PSDefaultParameterValues.('Invoke-Command:Credential') = (get-credential)

Now every time I run Invoke-Command it uses the credentials I specified earlier without having to ever use the -Credential parameter again. Will this feature make the final cut? I’m not sure. It does have  security implications. What if a rogue module developer attacked you by setting default parameters for a cmdlet? Then again, a rogue module developer could do a lot worse than this.  If nothing else,  there is certainly practical joke material here when we start modifying our friend’s profiles on April 1st. I’m curious. What do you think we can do with this feature? What cmdlets are screaming for this to be used in your profile?

Here’s my list:

  1. Every PowerCLI command Confirm equal to $false
  2. Perhaps a function that wraps invoke-command and Enter-PSSession so that every time it is run it sets the default value to the last computer you connected to.
  3. Get-ChildItem:Force = $true
  4. Write-Progress – I hate having to remember all of the parameters when I really just want to update one bit of activity and the percentage on the bar.
  5. Send-MailMessage – From, Subject, Body, SMTPServer – The beautiful thing is that you can set all of the defaults and override with the parameter as needed
  6. Default servers for your AD cmdlets of choice
  7. $PSDefaultParameterValues.(‘Select-Object:Property’) = ‘*’ ; This would let you do things like get-process |select #saving you two keystrokes over select *
  8. Verbose in any cmdlet that you think is appropriate
  9. Export-CSV and Out-File : Encoding = ASCII
  10. ErrorAction = SilentlyContinue – I put this one last as a joke.  Please people, don’t do this
What cmdlets and parameters do you think this makes sense for?  Please feel free to leave comments to discuss.
About these ads

One response to “PowerShell Default Parameters in v 3.0

  1. Jason Archer September 15, 2011 at 4:59 pm

    I’ve been waiting for that for a long while. :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 34 other followers

%d bloggers like this: