Saturday, August 18, 2012

PowerShell V3 - $PSDefaultParameterValues

One of the new features of PowerShell V3 that I find very helpful is having the ability to automatically pass default parameters to cmdlets or advanced functions.

Here are a few snippets of code that shows how to manipulate them.  Simply include your default parameters in your profile and script on!

# Intitial creation (perhaps place in profile)            
$PSDefaultParameterValues=@{            
"Send-MailMessage:SMTPServer"="smtp.osumc.edu";            
"Send-MailMessage:From"="Automated.Email@osumc.edu";            
"Get-Eventlog:logname"="Security";            
}            
            
# Add another one            
$PSDefaultParameterValues.Add("Send-MailMessage:Bcc","PhatBeard@osumc.edu")                  
# opps, need to modify the email address            
$PSDefaultParameterValues["Send-MailMessage:Bcc"]="FatBeard@osumc.edu"                       
# Add another one (straight out of Get-Help) that utilizes a scriptblock
$PSDefaultParameterValues+=@{"Format-Table:AutoSize"={if ($host.Name –eq "ConsoleHost"){$true}}}            
            
# Remove one            
$PSDefaultParameterValues.Remove("Get-Eventlog:logname")            
            
# Remove all of them            
$PSDefaultParameterValues.Clear()            
            
# Temporarily disable all parameters            
$PSDefaultParameterValues.Add("Disabled", $true)            
            
# Remove the temporary disable            
$PSDefaultParameterValues.Remove("Disabled")
Enjoy!

No comments: