Powershell

PowerShell Alias | A detailed guide

PowerShell Alias is an alternate name or substitute for the cmdlets. It is a user-defined short name that turns out to be helpful for remembering a particular command. Alias helps to reduce the lines of code, making it more readable and understandable.

We create PowerShell Alias by utilizing the Set-Alias cmdlet. After setting an Alias, memory will save overall details of the command with their Alias. As a result, you can call the command at any time with their specified Alias or print out the defined Aliases using the Get-Alias cmdlet.

This post will teach us how to create Alias in PowerShell.

How to create Alias in PowerShell?

The Set-Alias cmdlet is used in PowerShell to create an alias. In this cmdlet, specify the full name of the command for which you want to set an Alias. After setting Alias, utilize the Get-Alias command to print out the Alias.

Syntax
The syntax of Set-Alias in PowerShell is given as:

Set-Alias [-Name] <string> [-Value] <string>

Here, you have to add the Alias as <string> with the -Name option and the required command name as <string> with -Value option.

Example 1
Let’s check an example to set an alias of cmdlet Get-Location by using the above-defined syntax:

> Set-Alias -Name Location -Value Get-Location

Here, we will set the Alias Location for cmdlet Get-Location:

Now, you will be able to use the Get-Location command using the Location Alias. Moreover, if you want to verify the name of the defined Alias, execute the Get-Alias cmdlet:

> Get-Alias -Name Location

Example 2
In PowerShell, there are built-in Aliases for some commands that are used, such as ac for the Add-Content cmdlet and many more. To get the list of built-in Aliases, utilize the Get-Alias cmdlet:

> Get-Alias

In this example, we will modify the Alias write to wrOtpt for the Write-Output cmdlet:

To do so, we will use the Set-Alias cmdlet to overwrite the existing Alias:

> Set-Alias -Name wrOtpt -value Write-Output

Using Get-Alias cmdlet, verify the result of the previously performed operation:

> Get-Alias -Name wrOtpt

In the below snippet, you can see that the Alias write is now overwritten with wrOtpt Alias:

We have covered important details about PowerShell Alias. I hope it will be helpful for you.

Conclusion

Alias is a short title or an alternative name for the cmdlet. You can use the Set-Alias cmdlet for creating or changing any existing Alias. For this purpose, utilize the Set-Alias [-Name] <string> [-Value] <string> syntax, where you have to add the Alias as <string> with the -Name option and the required command name as <string> with -Value option. Then, verify the name of the defined Alias with the Get-Alias command. This blog demonstrated the method to create PowerShell Alias.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.