Powershell Best Practice #1: Use full cmdlet name (not alias)

By | May 22, 2015

Best Practice: It is recommended to use full cmdlet name, do not use alias in scripts.

Explanation: An alias is a an alternative shorter name for a cmdlet, function, script, file or executable file.
For instance, gc is the alias for the cmdlet Get-Content.

You can create an alias for a cmdlet using the following command : New-Alias -Name gssfvc -Value Get-SSFailoverVolumeContainers

You can list all the aliases (including the built-in aliases) with the cmdlet : Get-Alias

get-alias-powershell

You can find the equivalence alias <> cmdlet:

get-alias-definition

Note: PowerShell includes an alias provider (acts as a file system drive)

alias-powershell-psprovider

Aliases can be used for the interactive Powershell console (they are useful to speed up our work), but not in scripts.
It takes more time to write the full cmdlet but your code will be more clear and consistent.

Let’s take an example, you publish a script on Internet and you used aliases into your script, it will be probably downloaded and used by thousands of users, the problem is that we never know in advance what aliases are defined on a foreign computer. If someone decided to change a built-in alias to another command or made a mistake when setting an alias, then the behaviour of the script becomes unpredictable and can lead to unexpected behaviour.

Moreover, some aliases are not so intuitive and it can be difficult to remember all of them.

get-alias-not-obvious


next-button

One thought on “Powershell Best Practice #1: Use full cmdlet name (not alias)

  1. Pingback: Powershell Best Practice #2: Use named parameter (not positional and partial parameter) | Powershell Guru

Leave a Reply

Your email address will not be published. Required fields are marked *