Auto-Declaring Alias Names for Functions

by Apr 5, 2017

You probably know that PowerShell supports alias names for commands. But did you know that you can define alias names for PowerShell functions inside a function definition (introduced in PowerShell 4)? Have a look:

function Get-AlcoholicBeverage
{
    [Alias('Beer','Drink')]
    [CmdletBinding()]
    param()

    "Here is your beer."
}

The “official” name for the function is Get-AlcoholicBeverage, but this function is also available via the aliases “Beer” and “Drink”. PowerShell automatically adds these aliases when the function is defined:

 
CommandType     Name                                              
-----------     ----                                                                                       
Alias           Beer -> Get-AlcoholicBeverage                                                                               
Alias           Drink -> Get-AlcoholicBeverage

Twitter This Tip! ReTweet this Tip!