Introduction to PowerShell Alias
Alias in PowerShell allows us to define short names for commands so that it will be easy for us to remember and write it, we can also create alias for any executable files.
When we write any command and use the Set-Alias along with the name which we wanted to use for shorter names for the commands than in memory it will hold the command full details inside the memory with the Alias name. At any time if we wanted to use the defined shorter name for the alias we can call it with Get-Alias. We can use Set-Alias and Get-Alias commands for setting and getting the actual command with Alias. We also have an available command for removing and any existing Alias. We can overwrite the existing Alias if we wanted to do any modification into the existing Alias.
How to Create PowerShell Alias?
To create an alias we need to use the Set-Alias command. This command allows us to set the alias name for the given function. Suppose we wanted to see the all list of available packages than we can use the command Get-Package but here we will create an alias for Get-Package. See the example below, here in this example we are setting an alias name for Get-Package with “pkg” name and in the next line, we are getting details of it by Get-Alias with “pkg” name.
We can use below syntax for setting an alias in powershell.
[-Name] <shorter name for command>
[-Value] <actual command which we wanted to shorter>
[-Description <String description of what command will do>] [-Option <access level of command like readonly ,read and write both etc>] [-PassThru<details of object through which it is going through>] [-Scope <string value of scope like local or global>] [-Force<we can use it for forced for hidden files>] [-WhatIf<display what will happen without actual impact>] [-Confirm<display a prompt for your confirmation>]
Below is the example to set an alias in PowerShell.
Set-Alias -Name pkg -Value Get-Package
In the above example, we have set an Alias to the command Get-package, and with the help of Get-Alias on the defined alias name, we are able to get the alias details.
How to Access and Modify PowerShell Alias?
Here we discuss how to access and modify PowerShell alias.
Accessing Alias in PowerShell
To access Alias in PowerShell we can use a command called Get-Alias, it will allow us to get the name set by a command called Set-Alias. Below is an example to get the alias name. To access any defined Alias in PowerShell we can use below format,
[[-Name] <array of string name which we set in alias>] [-Exclude <We can exclude with writing array of string>] [-Scope <String value of scope like local or global>]
Set-Alias -Name TP -Value ./ranjan/ranjan1
In PowerShell, if we wanted to modify any alias then we can overwrite it again with another value and it will be modified, which will change the older name of the alias. In general, there is not any available command for modification we need to override the existing alias value with the new one.
Setting an alias and getting it.
Set-Alias -Name TPF -Value ./ranjan/ranjan1
Modifying existing alias with a new value.
Set-Alias -Name TPF -Value ./ranjan/ranjan2
In the above example first, we set the value for the Alias “TPF” as the path “/ranjan/ranjan1” and in the next line we change it to “ranjan/ranjan2” by rewriting the same command with different values.
How to Remove PowerShell Alias?
We can remove any Alias with the PowerShell command called Remove-Alias. This command removes the Alias with the name which we passed for it. See the below format to remove Alias in PowerShell.
[-Name] <String name for the Alias which we want to remove>
[-Scope <String scope name like local>] [-Force<In case we do not have permission or it is hidden than we can run this command to work>]
Set and get the alias,
Set-Alias -Name TPF -Value ./ranjan/ranjan1
Remove the alias defined above,
Remove-Alias -Name TPF //removing the the Alias “TPF”
Get-Alias TPF//will through an error as we have removed the Alias “TPF”
On running the command get-Alias it will be through an error as we have already removed the Alias “TPF” and we are trying to fetch its value.
Let’s understand one thing when we will be writing any big script at that time our code goes very long and maintenance is very hard in that case. Also, the readability of code will not be very good. For example, if we wanted to write some command related to all running processes than in PowerShell we will use “Get-process” but with the help of Alias concept in PowerShell, we can write the same command in more readable like “process-list” or “PL”.by writing “PL” instead of writing “Get-process” we are able to save few characters. Here the length of “Get-process” is 11 and the length of “PL” is two so we are able to save 9 letters here.
In general, we can define a few points for its importance they are given below.
- Shorter the Code: Because it is in our hands what name we wanted to give to the PowerShell command for an alias, so we can give smaller names to them, which can make our code less than half of the code without an alias.
- More Readable the Code: Many commands do not make very meaningful names according to the work they perform, so in such a situation we can define more readable Alias name for commands so that others and we also can easily be able to understand the code.
- Easier to Maintenance: Because code is more readable and understandable, other people who are going to some work on an existing script they can easily understand each line and their meaning
This is a guide to PowerShell Alias. Here we discuss the Introduction and how to create PowerShell alias and how to access and modify PowerShell alias with its importance. You may also look at the following articles to learn more –