Updated June 20, 2023
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 them; we can also create an alias for any executable files.
When we write any command and use the Set-Alias along with the name we wanted to use for shorter names for the commands, then in memory, it will hold the command’s full details inside the memory with the Alias name. If we want 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 an alias. There is also a command available to remove any existing Alias. We can overwrite the existing alias if we want to do any modifications to 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 all list of available packages, then 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 the “pkg” name, and in the next line, we are getting details of it by Get-Alias with the “pkg” name.
We can use the below syntax for setting an alias in Powershell.
Syntax:
Set-Alias
[-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 an example of setting an alias in PowerShell.
Example:
Set-Alias -Name pkg -Value Get-Package
Get-Alias pkg
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 can get the alias details.
How to Access and Modify PowerShell Alias?
Here we discuss how to access and modify the 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 the below format,
Syntax:
Get-Alias
[[-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>]
Example:
Set-Alias -Name TP -Value ./ranjan/ranjan1
Get-Alias TP
Output:
Modify Alias
In PowerShell, if we want 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. There is no modification command in general; we need to override the existing alias value with the new one.
Setting an alias and getting it.
Example:
Set-Alias -Name TPF -Value ./ranjan/ranjan1
Get-Alias TPF
Output:
Modifying existing alias with a new value.
Example:
Set-Alias -Name TPF -Value ./ranjan/ranjan2
Get-Alias TPF
Output:
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.
Syntax:
Remove-Alias
[-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,
Example:
Set-Alias -Name TPF -Value ./ranjan/ranjan1
Get-Alias TPF
Output:
Remove the alias defined above,
Example:
Remove-Alias -Name TPF //removing the the Alias “TPF”
Get-Alias TPF//will through an error as we have removed the Alias “TPF”
Output:
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.
Importance
Let’s understand one thing: when we write any big script, our code goes very long, and maintenance is very hard in that case. Also, the readability of the code will not be very good. For example, if we wanted to write some command related to all running processes, then in PowerShell, we will use “Get-process.” Still, with the help of the Alias concept in PowerShell, we can write the same command in a more readable like “process-list” or “PL” .by writing “PL,” instead of writing “Get-process,” we can save a few characters. Here the length of “Get-process” is 11, and the size of “PL” is two, so we can save 9 letters here.
In general, we can define a few points for their importance below.
- Shorter the Code: Because it is in our hands what name we want to give to the PowerShell command for an alias, we can provide them smaller names, making our code less than half the code without an alias.
- More Readable Code: Many commands do not make significant names according to their work. In such a situation, we can define more readable Alias names for commands so that others and we can easily understand the code.
- Easier to Maintenance: Because code is more readable and understandable, other people working on an existing script can easily understand each line and their meaning.
Recommended Articles
This is a guide to PowerShell Alias. Here we discuss the Introduction and how to access and modify a PowerShell alias with its importance. You may also look at the following articles to learn more –