Powershell

How to Create PowerShell Alias

PowerShell supports a wide range of cmdlets and functions that assist in performing several tasks. These cmdlets and functions have specific built-in keywords that are used to execute them from the shell. Moreover, PowerShell allows you to execute any function/cmdlet by using any alternate keyword, and this alternate keyword is known as alias

There are few functions/cmdlets that have built-in aliases such as Clear and Cls serve as aliases for the Clear-Host function of PowerShell. Apart from these built-in aliases, users can also create an alias for a function/cmdlet. So, we have prepared this demonstration to guide you on creating an alias in a PowerShell.

How to create PowerShell Alias

PowerShell supports a set of built-in aliases for several functions/cmdlets. Firstly, start with getting the available aliases in your PowerShell. To do so, the command written below will assist in this regard.

> Get-Alias

However, to create a new alias you have to adopt one of the following syntaxes:

> Set-Alias -Name <name-of-alias> -Value <name-of-the-command>
> New-Alias -Name <name-of-alias> -Value <name-of-the-command>

In the above syntaxes, the -Name and -Value parameters refer to the name of the alias and the cmdlet/function that you want to associate with that alias.

How to create new alias using `Set-Alias`

This section refers to creating an alias by using the Set-Alias cmdlet of PowerShell.

Example 1
The command provided below will set an alias to the Get-Variable cmdlet of PowerShell. We have set the alias name as var :

> Set-Alias -Name var -Value Get-Variable

After creating an alias, you can verify it as we did in the following command and the output shows that it has been successfully mapped for the Get-Variable cmdlet.

> Get-Alias var

The above syntaxes do support several other parameters that can be used to get an alias of a specific property.

Example 2
In the above example(Example 1), we have created an alias var and linked it to Get-Variable. The following command will set the var alias to another cmdlet named Clear-Variable. The output shows that the command has been executed successfully.

> Set-Alias -Name var -Value Clear-Variable

Now, look for the association of var Alias by using the command provided below: It is observed from the output that the var is no longer linked to Clear-Variable cmdlet. The var variable was associated with Get-Variable cmdlet in Example1 and when setting var as an alias for Clear-Variable cmdlet, the Set-Alias just changed the cmdlet for var alias.

> Get-Alias var

From Example 1 and Example 2, it is concluded that if the existing alias is used again, then Set-Alias will replace the association of that alias.

How to create an alias using `New-Alias`

The New-Alias is another PowerShell cmdlet that can be used to add a new alias to your current session of PowerShell. This section contains a few examples that better clarify the concept of the New-Alias cmdlet:

Example 3
Following the syntax of New-Alias, we have created a new alias named show and linked it with Get-Process cmdlet. After successful execution, we have verified the creation and association of show  alias:

> New-Alias -Name show -Value Get-Process

Example 4
Talking about the existing aliases, the New-Alias act differently when compared with Set-Alias. For instance, we want to associate the show with the Get-Service cmdlet using New-Alias. For that, the command stated below is executed. After execution, an error will appear displaying that the show alias exists already.

> New-Alias -Name show -Value Get-Service

How to create a permanent alias in PowerShell

It is observed that the Set-Alias and New-Alias create aliases for the present environment of PowerShell and when the session is refreshed you won’t be able to execute the aliases of the previous session. Follow the steps to create an alias that will work for each session.

Step 1: Create and export the aliases as per your need in the current session
Firstly, create the aliases using New-Alias/Set-Alias in the current session. Once you are done with creation, you have to export these aliases to a file, so that they can be imported in any session. To export the aliases, the Export-Alias cmdlet of PowerShell is used.

Note: The path needs not be the same as we used. You can create the aliases export file at any location on your machine.

> Export-Alias C:\Temp\Aliases

Step 2: Import the file
The exported file is then reused to get the functionality of those aliases. To import the file you need to insert the correct path where it was exported. For instance, in our case the Aliases file is at C:\Temp\Aliases and is imported using the following command:

> Import-Alias C:\Temp\Aliases -Force

The Import-Alias tries to import the built-in aliases as well and these built-in aliases produce errors because they already exist. To avoid these errors, the -Force option is practiced in the above-mentioned command.

Conclusion

A single PowerShell function/cmdlet can contain multiple aliases, but a single alias can only be associated with one function/cmdlet. In this demonstration, you have learned the potential ways to create a PowerShell alias. We have concluded that the Set-Alias can create as well as can change the association of existing aliases. Whereas the New-Alias throws an error if the same alias already exists. Moreover, we have also provided a way to create permanent aliases and these aliases can be imported in each PowerShell session.

About the author

Adnan Shabbir