How to Create Aliases in PowerShell
- Create Aliases in Windows PowerShell
- Are PowerShell Aliases Saved?
- Listing All Existing PowerShell Aliases
- Exporting and Importing Windows PowerShell Aliases
- Remapping and Removing Windows PowerShell Aliases
- Potential Aliasing Issues
The Windows PowerShell alias is another name for the cmdlet or command element. Windows PowerShell can create aliases for PowerShell cmdlets if we forget their names. A Windows PowerShell alias lets us find cmdlets using a more straightforward, shorter string of characters than the whole command. This article will discuss how we create our aliases and set them up correctly in our environment.
Create Aliases in Windows PowerShell
To create an alias in Windows PowerShell, we will use the New-Alias
cmdlet. The New-Alias
cmdlet creates a new alias in the current session.
New-Alias Goto Set-Location
When we type an Alias in a PowerShell window or use an alias in scripting, Windows PowerShell will know to execute the cmdlet mapped to the alias.
Are PowerShell Aliases Saved?
If we close a Windows PowerShell window session, the aliases we have made with the New-Alias
cmdlet are not saved. Therefore, we will need to recreate the same Windows PowerShell aliases if we have aliases set up in our scripts.
When we run the script, and the engine fails to map aliases with any cmdlets, it will throw errors. Therefore, we must use Set-Alias
instead of the New-Alias
PowerShell cmdlet to prevent these errors from happening. Then, follow the steps below to ensure that Aliases remain active when we open a new Windows PowerShell window or when a PowerShell script runs that use aliases.
- Create a PSConfiguration folder in our Windows PowerShell profile. We can find out our Windows PowerShell Profile by running the
Get-Variable Profile
PowerShell cmdlet. - Inside the PSConfiguration folder, create a file named
Microsoft.PowerShell_Profile.PS1
. This file will contain the Windows PowerShell cmdlets to create aliases. - Next, add the PowerShell commands to create aliases in the file.
Set-Alias Goto Set-Location
Listing All Existing PowerShell Aliases
If we forget the name of a saved alias, we can get the list of all existing Windows PowerShell aliases by using the Get-Alias
PowerShell command. When we execute the Get-Alias
cmdlet, it retrieves all aliases created, including those made from the Microsoft.PowerShell_Profile.PS1
file. So, for example, if we wish to retrieve a specific alias that we made, we can use the -Name
parameter with the Get-Alias
command as shown below:
Example Code:
Get-Alias -Name GoTo
Exporting and Importing Windows PowerShell Aliases
Using the above method will ensure Windows PowerShell Aliases remain active on the system, but we may also need to export and import PowerShell Aliases. This method is proper when reinstalling a Windows Operating System or encountering issues with the PowerShell engine. Execute the following PowerShell command to export all PowerShell Aliases from a Windows PowerShell profile.
Example Code:
Export-Alias C:\temp\PSAliases.txt
The above command exports all Windows PowerShell Aliases in a comma-separated values list to the PSAliases.txt
file. If we wish to export all aliases as a PowerShell script, we can use the -As Script
switch parameter with the command as shown in the below example:
Example Code:
Export-Alias C:\temp\PSAliases.txt -As script
Importing Windows PowerShell Aliases from a file is easy. Just execute the PowerShell command to start the Alias import process:
Example Code:
Import-Alias C:\temp\PSAliases.txt
Tip: When the created script imports aliases from the file, the script doesn’t overwrite previously created aliases.
Remapping and Removing Windows PowerShell Aliases
Remapping an existing alias to a different Windows PowerShell cmdlet can be done using the Set-Alias
cmdlet, as shown in the example below:
Set-Alias Goto Set-Location
Microsoft has not yet developed a separate cmdlet to remove Windows PowerShell Aliases. However, we can use the Remove-Item
command with multiple applications. You will need to set the Windows PowerShell parameter to Alias:
as shown in the following command:
Remove-Item Alias:Goto
If we wish to remove all Aliases from the PowerShell profile, execute the Remove-Item Alias:*
cmdlet.
Potential Aliasing Issues
Using Windows PowerShell Aliases has its benefits, but there are also some potential aliasing issues to be aware of:
- Avoid adding too many aliases to our profile to prevent confusion. If we add too many aliases, we run into the same problem we had previously of not remembering all of our most used cmdlets.
- Aliasing long, multiple command strings will cause the command to fail.
- Reserve our PowerShell aliases for only single commands or cmdlets
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedIn