Don’t you get bored of typing long commands? Yes, I feel discomfort while typing, but what can I do? It is necessary for the completion of my task.
Yeah, I understand, but why don’t you set an alias for the long command, which will be helpful for you to invoke the long command in just a few keystrokes?
Sounds great, but I’m not aware of how to set aliases in PowerShell. Will you guide me on how to assign aliases to some of the common long cmdlets in PowerShell?
Of course, I will show you how to create temporary aliases and permanent aliases in PowerShell, but before that, let’s learn how to check all the available aliases in your system.
List all the available aliases in PowerShell
PowerShell has a number of built-in cmdlets to use, such as
chdir, and many more, which is very helpful for users who are using Linux or Unix-like systems along with Microsoft PowerShell.
To learn about the available aliases, run the below cmdlets on your PowerShell window to list out all the built-in aliases.
The output of the above command
CommandType Name Version Source
----------- ---- ------- ------
Alias % -> ForEach-Object
Alias ? -> Where-Object
Alias ac -> Add-Content
Alias asnp -> Add-PSSnapin
Alias cat -> Get-Content
Alias cd -> Set-Location
Alias CFS -> ConvertFrom-String 22.214.171.124 Microsoft.PowerShell.Utility
Alias chdir -> Set-Location
Alias clc -> Clear-Content
********* SNIPPED ***********
Alias stz -> Set-TimeZone 126.96.36.199 Microsoft.PowerShell.Management
Alias sujb -> Suspend-Job
Alias sv -> Set-Variable
Alias swmi -> Set-WmiInstance
Alias tee -> Tee-Object
Alias trcm -> Trace-Command
Alias type -> Get-Content
Alias wget -> Invoke-WebRequest
Alias where -> Where-Object
Alias wjb -> Wait-Job
Alias write -> Write-Output
After getting aware of how to list all the aliases that are available on your system, let’s jump to the creation part, where you will learn how to create an alias.
Create Aliases in PowerShell
You can create aliases for two types. One of them is to create temporary aliases that will only be valid for the current shell. Once the shell is closed, you will lose all the aliases you created, and to avoid that, you can use permanent aliases.
And we will show you both the methods in this article, so let’s first start with creating temporary aliases to get familiar with the New-Alias and Set-Alias cmdlets in PowerShell.
Create a Temporary Alias in PowerShell
To create a temporary alias in PowerShell, you can use the New-Alias and Set-Alias cmdlets, which will create an alias on your system, and after creating an alias, you can use the same created alias in the current shell to invoke and execute the cmdlets.
And to start the process of creating an alias for your custom needs, first, get acquainted with the New-Alias and Set-Alias syntax.
New-Alias -Name [ EnterAliasName ] -Value [ EnterAliasValue ]
Set-Alias -Name [ EnterAliasName ] -Value [ EnterAliasValue ]
For instance, let me show you how to create a temporary alias using the above syntax.
To explain it to you in a better way, I have selected the
Get-ComputerInfo cmdlets that give system information in a very subtle way, so I thought instead of typing
Get-ComputerInfo, why not create an alias of it by calling it cmpi?
New-Alias cmpi Get-ComputerInfo
Set-Alias cmpi Get-ComputerInfo
Once you execute the above command, it will create a temporary alias in your system, which you can check by running
Get-Alias or simply executing the alias on a shell like in the below snippet.
WindowsBuildLabEx : 19041.1.amd64fre.vb_release.191206-1406
WindowsCurrentVersion : 6.3
WindowsEditionId : Professional
WindowsInstallationType : Client
WindowsInstallDateFromRegistry : 09-09-2022 19:09:45
Now you might be thinking about what the major difference between New-Alias and Set-Alias is and which we should prefer to create an alias. Yes, I was also a bit skeptical about New-Alias and Set-Alias, but once I read the manual, I got the idea behind it.
New-Alias should be used when you want to create a new alias, and Set-Alias can be used to create a new alias along with updating an older alias with the new value.
As you know, this method will only be applicable to the current shell, and once you close the shell, all the created aliases will get wiped out. Secondly, temporary aliases will not accept combinations of multiple cmdlets, pipelines, and other options.
To fix this problem, you need to create a permanent alias, which you will see in the next section.
Create a Permanent Alias in PowerShell
After getting aware of the New-Alias and Set-Alias cmdlets, let’s use them to create a permanent alias in PowerShell. For that, you need to have administrative permission to allow PowerShell to access and implement scripts.
Step 01: Create a PowerShell profile
To create or open a PowerShell profile configuration, run the below command.
Step 02: Add alias information
After that, populate your PowerShell profile with your custom alias information. To add alias information, you can take a reference to the below code snippet.
# some functionvalue
Set-Alias MyAliasName Some-FunctionName
To explain it to you better, let me create a short function where I’ll put the information to connect my virtual machine through ssh with the public key, and I can easily invoke the ssh connection by typing “to”.
ssh -t -i 'C:\Users\gagan\Keys\id_ed25519' [email protected]
Set-Alias to connect-TO
Step 03: Signed profile script for execution
Once you end up creating aliases, save the file and restart the shell to activate the changes, or else load the profile directly by executing the below code.
And if you get an error like “cannot be loaded because running scripts is disabled on this system.”, then you need to change the execution policy, which requires administrator privilege.
So to change the permission, run PowerShell in administrator mode and execute the below command, which will prompt you for confirmation, so simply press Y to agree to the risk of allowing the script.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
To learn more about the execution policy refer to this article.
That’s all for this article! I hope you are able to create an alias on your system.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.