PowerShell was created to help server admins efficiently manage Windows Server roles and features. Most of the PowerShell cmdlets starts with “Get,” “Set,” and “Remove” words.
PowerShell cmdlets starting with “Get” word can be used to collect information from specific roles and features. PowerShell cmdlets starting with “Set” word are used to modify information on specified targets, and “Remove,” as the name implies, can be used to perform removal operations on target systems.
While the PowerShell Intellisense feature is smart enough to complete the name of a PowerShell cmdlet with a press of the “Tab” key, you need to keep pressing the “Tab” key until you find the cmdlet you are looking for. One of the best things PowerShell offers is the ability to create aliases for PowerShell cmdlets if you have problems remembering the names of PowerShell cmdlets.
What a server admin can do is create Aliases for PowerShell cmdlets that are hard to remember. For example, you could create a PowerShell Alias to enable you to type “List” instead of needing to type the full “Get-ChildItem” PowerShell cmdlet. Similarly, you may want to use “MBStat” instead of typing “Get-MailBoxStatistics.”
How Do I Create an Alias for a PowerShell Cmdlet?
It’s quite easy to create an Alias for a PowerShell cmdlet. You will be required to use the “New-Alias” PowerShell cmdlet to create an Alias for a PowerShell cmdlet. Here are a few examples of using New-Alias PowerShell cmdlet:
New-Alias AzureMB Get-MailBoxStatistics
New-Alias List Get-ChildItem
New-Alias ConnectOnline Connect-MSolService
New-Alias ConnectAzure Connect-AzureAD
And so on…
Once you are done mapping Aliases to PowerShell cmdlets, when you type an Alias in a PowerShell window or use an Alisa in PowerShell scripting, PowerShell will know that it needs to execute the PowerShell cmdlet that is mapped to the Alias.
Are PowerShell Aliases Saved?
By default, PowerShell Aliases are not saved if you close a PowerShell window session. For example, if you create a few Aliases and close the PowerShell window, you will be required to recreate the same PowerShell aliases. This would obviously present a problem if you have Aliases set up for use in PowerShell scripts.
When you run the PowerShell script and the PowerShell engine fails to find a mapping of aliases with any PowerShell cmdlets, it will throw errors. What you can do to prevent these errors is use “Set-Alias” instead of using the “New-Alias” PowerShell cmdlet and then follow the below steps to ensure that Aliases remain active when you open a new PowerShell window or when a PowerShell script runs that uses Aliases:
- Create a PSConfiguration folder in your Windows PowerShell profile. You can find out your Windows PowerShell Profile by running the “Get-Variable Profile” PowerShell command.
- In the PSConfiguration folder, create a file named “Microsoft.PowerShell_Profile.PS1.” This file will hold the PowerShell commands to create aliases.
- Next, add the following PowerShell commands to create aliases in the file. For example, if you need the above Aliases to be active in your PowerShell session, you will need to add these lines in the “Microsoft.PowerShell_Profile.PS1” file:
Set-Alias AzureMB Get-MailBoxStatistics
Set-Alias List Get-ChildItem
Set-Alias ConnectOnline Connect-MSolService
Set-Alias ConnectAzure Connect-AzureAD
Listing All Existing PowerShell Aliases
If you forget the name of a saved Alias, you can always retrieve the list of previously created Aliases by using the “Get-Alias” PowerShell command. When you execute the “Get-Alias” cmdlet, it retrieves all aliases that have been created including those that were created from the “Microsoft.PowerShell_Profile.PS1” file. If you wish to retrieve a list of Aliases that start with a specific character, you can use the “-name” parameter with the “Get-Alias” cmdlet as shown in the example below:
Get-Alias –Name R*
Get-Alias –Name D*
Get-Alias –Name AZ*
Exporting and Importing PowerShell Aliases
While you can use the above method to ensure PowerShell Aliases remain active on the system, you may also find that you need to export or import Aliases in case you need to reinstall Windows Operating System or you encounter issues with the PowerShell engine. To export all PowerShell Aliases from a Windows PowerShell profile, execute the following PowerShell command:
The above command exports all Aliases in a comma-separated values list to the “C:\Backup\PowerShellAliases.TXT” file. If you wish to export all Aliases as a PowerShell script, you can use the “-As Script” switch with the command as shown in the below example:
Export-Alias C:\Backup\PowerShellAliases.TXT –As “Script”
Importing PowerShell Aliases from a file is easy. Just execute this PowerShell command to start the Alias import process:
Tip: When PowerShell imports aliases from a file, it doesn’t overwrite already created aliases.
Remapping and Removing Aliases
Remapping an existing Alias to a different PowerShell cmdlet can be done using the Set-Alias cmdlet as shown in the example below:
Set-Alias Disp Get-ChildItem
Microsoft has not developed a separate cmdlet to remove PowerShell Aliases, but you can use the Remove-Item cmdlet, which is also used for other purposes. What you need to do is set PowerShell context to “alias:\” as shown in the following command:
If you wish to remove all Aliases from PowerShell profile, simply execute a “Remove-Item Alias:\*” command.
Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at email@example.com.