Windows PowerShell aliases are very handy. They typically have fewer characters than the commands and parameters that they represent, so they’re quicker to type and great for tweeting. However, it has long been an established best practice that aliases should not be used in blogs, in help, in examples, or in any shared scripts and commands. They create an extra layer of confusion for beginners and they’re an obstacle for anyone who needs to maintain the script.
Still, we use them. I know that, despite my best efforts, I type “dir” and “where” and “gcm” and “ipmo” and “sls” and, worst of all, “foreach” when I mean the ForEach-Object cmdlet, not the ForEach statement.
Fortunately, some nifty features in PowerShell Studio 2014 make it easy to conform to the best practice, despite our habits.
- When you type an alias, PrimalSense shows that it’s a alias.
- When you type a cmdlet alias or parameter alias and then press TAB, PowerShell Studio expands it. (You can disable this feature in Options\Editor\Enable alias tab expansion.)
- When you type a partial parameter name (such as –m or –mod for –Module) and a space, PowerShell Studio expands it.
- To expand an alias, right-click and click Expand to Cmdlet or Expand Parameter.
But, when I’m scripting, I’m a bit too focused to think about aliases. Instead, when I’m done with my script, just before I sign it, I type:
CTRL + SHIFT + A
Ctrl+Shift+A converts all cmdlet and parameter aliases, and parameter partial names*, to their full names with correct capitalization. What an easy way to support a best practice.
Oh, and in case you’re wondering, PowerShell Studio 2014 is happy to undo all of your good deeds. You can right-click and click Convert to Alias or type Ctrl+Shift+Alt+A to convert all cmdlet and parameter names to aliases.
A few notes * :
- Beginning in PowerShell Studio 2014 version 4.1.72, Ctrl+Shift+A expands aliases and Ctrl+Shift+Alt+A contracts names to their aliases. In earlier versions, Ctrl+Shift+Alt+A expands aliases and Ctrl+Shift+A contracts them.
- Also, beginning in version 4.1.72, Ctrl+Shift+A expands cmdlet aliases, parameter aliases and partial parameter names. In earlier versions, Ctrl+Shift+Alt+A expands only cmdlet aliases.
- The alias expansion features don’t expand aliases that you have defined in your profile. In best practice fashion, just like your users, the PowerShell Studio Editor window does not have access to your profile.
- The alias expansion features don’t recognize aliases that you have defined in your script. (You really shouldn’t define aliases in a script anyway.)