Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Make Defaults a Way of Life

Posted on April 8, 2021April 8, 2021

A quick post today to remind you of a way to make PowerShell even easier to use. PowerShell cmdlets and functions obviously help us get a lot done, and most commands offer a number of parameters to customize what needs to be done. Unless you love typing, you probably would like an even easier way to use PowerShell. Let me show you.

Here's a command that I use often.

But I don't usually care about anything other my fixed drives, C and D. Get-Volume makes that easy.

But I'm lazy. Even with tab-completion I need to remember to use these parameters. Why not make them the default? That's where $PSDefaultParameterValues comes into the picture. This is an automatic variable that is a hashtable. The key takes the form "command:parameter".

$PSDefaultParameterValues.add("Get-Volume:driveletter",@("c","d"))

By the way, you can use wildcards. Here's a setting I use so that all ActiveDirectory commands automatically connect to DOM1.

$PSDefaultParameterValues.Add("*-AD*:Server", "DOM1")

If I don't specify the parameter and a different value, PowerShell will use this default. Now, Get-Volume is even easier.

I put the command in my PowerShell profile script and I don't have to worry about it. It is all of these little savings that add up over the course of a day or week, and make PowerShell less of a chore to use.

Share this:

  • Print (Opens in new window) Print

Like this:

Like Loading…

Related

4 thoughts on “Make Defaults a Way of Life”

  1. Pingback: Setting Default Parameters in Powershell – Curated SQL
  2. Pingback: ICYMI: PowerShell Week of 09-April-2021 – PowerShell.org
  3. Ivan says:
    April 9, 2021 at 9:07 pm

    Oh! I’m using this!!!! – Nice tip.

  4. Pingback: More About PowerShell PSDefaultParameterValues • The Lonely Administrator

Comments are closed.

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2026 The Lonely Administrator | Powered by SuperbThemes!
%d