header image

Using aliases in scripts

Posted by: | July 23, 2019 Comments Off on Using aliases in scripts |

There’s been a long debate on the PowerShell github site regarding ternary operators – think of it as a short cut for if-else. Twice in that debate the point has been made that aliases are perfectly acceptable in scripts. Wrong. Using aliases in scripts should never be encouraged.

 

Aliases are short cuts to cmdlet and parameter names. In some instances a parameter name has changed but the old name is available as an alias so existing code doesn’t break.

 

Using aliases at the command line is perfectly acceptable as they cut down typing – debateable with PSUseAbbreviationExpansion in PowerShell v6/7 – but I can see their use in that situation. I tend to use full cmdlet names – more from habit due to doing so many demos, articles and books – but understand that many people use aliases.

 

Aliases in scripts are a bad idea because they cause confusion. They’re more difficult to understand, especially for new comers, and make code harder to maintain especially for someone other than the original writer.

 

If you use VScode or ISEsteroids they will explicitly flag aliases to be expanded. PSScriptAnalyzer (also used in VScode) will also flag aliases that should be expanded.

 

Given tab completion and the intellisense built into VScode and ISE using aliases in your code is just bad practice. There is no need to do so and very good reasons why you shouldn’t.

under: PowerShell

Comments are closed.

Categories