PowerShell Default Parameter Values – Time to Tweak Your Profile Again!
One of the great things about a PowerShell profile is you get to customize the environment. One nice feature to accomplish this is the ability to set default parameter values for cmdlets. A great write-up about this is available at About Parameters Default Values. You can see similar info by running get-help about_Parameters_Default_Values. I’m not going to go into great detail about the feature, as those two resources are more than sufficient. What I am going to list here are some great examples that I’ve come to love. They are all in my profile.
This first example if my favorite. When you use the Get-Help cmdlet, the resulting info is shown in a popup window. This is great as I can keep it open while I work on my code in the main console window.
$PsDefaultParameterValues.add("Get-Help:ShowWindow",$True)
If you’re an OCD type like me, you want output of commands to be formatted and everything to line up. This example defaults the Format-Table cmdlet to autosize its output.
$PsDefaultParameterValues.add("Format-Table:AutoSize",$True)
We can actually set this behavior for both Format-Wide and Format-Table at the same time (courtesy of TechNet):
$PSDefaultParameterValues.add("Format-[wt]*:Autosize",$True)
How about we capture the output of the last PowerShell command into a variable, such as $0 (courtesy of TechNet):
$PSDefaultParameterValues.add("Out-Default:OutVariable",“0”)
If you can’t tell by my other articles and scripts here, I spend a lot of time writing scripts to help in deployments, migrations, etc. When you’re getting or setting data in Active Directory, the last thing you want is for replication (intersite or intrasite) to be an issue. So, when possible, you specify a specific domain controller to send all of your commands together. This example comes from serverfault:
$PSDefaultParameterValues = @{"*-AD*:Server"='YOUR-CHOSEN-DC'}
Now that’s not completely perfect, as it would have a hard coded DC name. And just our luck, it will be migrated out of existence and then our stuff breaks. So, let’s set a default parameter value with the result of a PowerShell query. In this case, a DC in the same site for each of our Lync/Skype for Business cmdlets that support the Server parameter:
$PSDefaultParameterValues.add("*-Cs*:Server",(Get-ADDomainController -Discover -NextClosestSite))
Or maybe you want to specify the PDC emulator instead? (courtesy Tommy Maynard)
$PSDefaultParameterValues.Add("Get-ADPrincipalGroupMembership:Server",$((Get-ADDomain).PDCEmulator))
Or, we can use a variable. Let’s say we assign $DC to a DC in the same site, and then use that going forward:
$dc = Get-ADDomainController -Discover -NextClosestSite $PSDefaultParameterValues.add("*-AD*:Server","$dc") $PSDefaultParameterValues.add("*-Cs*:Server","$dc")
Now, how many times do you get the “Are you sure” prompt? Force the command instead of getting prompted!
$PSDefaultParameterValues['*:Force']= $true
Fellow MVP Boe Prox also has a great list of examples on his post Using $PSDefaultParameterValues in PowerShell. Check it out!
Now, you can list each of these, or any combination, in your profile, each on a separate line. Or, we can use an array to set all at one time:
$PSDefaultParameterValues=@{ 'Format-Table:AutoSize'=$True; 'Get-Help:ShowWindow'=$True; 'Send-MailMessage:SmtpServer'=$smtpserver }
Something to keep an eye on here. In previous examples, I was using the $PSDefaultParameterValues.add method which ADDS the value to the existing list. If you omit the “.add” and instead use “=” or “=@{}”, you are replacing all existing values with what you specify. Additionally, you can use the $PSDefaultParameterValues.remove method to remove specific parameter values and keep any remaining values. An example of removing a single default parameter value:
$PsDefaultParameterValues.remove("Get-Help:ShowWindow",$True)
Are all of these changes permanent? No. They are valid for the life of the PowerShell session. If you need to remove them mid-session, you can clear them using:
$PsDefaultParameterValues.clear()
Defining default parameter values can also be defined at various scopes, as well, including Global, Script, etc. See Get-Help about_Scope for more info.
I mentioned at the beginning that these are great in your profile. Well, they’re great in your scripts as well. They allow for global changes instead of going through a script and updating each call to a cmdlet. By all means, send me your favorites. We’ll build a big list!
Follow Me