Guides Harnessing the Power of PowerShell Aliases

Harnessing the Power of PowerShell Aliases

Creating aliases for PowerShell cmdlets can be a valuable tool for saving you time and headaches over remembering the names of commands. PowerShell was created to help server admins efficiently manage Windows Server roles and features. These PowerShell cmdlets can be used to collect information from specific roles and features, modify information on specific targets and perform removal operations on target systems.

Windows Server Tutorials

One issue with PowerShell is that it can be tedious to find the specific cmdlets you’re looking for. Even using the PowerShell Intellisense feature requires you to cycle through a long list of cmdlets until you find the right one. 

Fortunately, PowerShell offers the ability to create aliases for PowerShell cmdlets if you have problems remembering the names of them. A PowerShell Alias enables you to find cmdlets using a simpler, shorter string of characters than the full command. For example, you could create a PowerShell Alias called “List” in place of the full cmdlet of “Get-ChildItem.”

How Do I Create an Alias in PowerShell?

So how do you create an Alias in PowerShell? It’s actually quite easy to create a PowerShell Alias cmdlet. You’ll use the “New-Alias” 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…

Now that you’ve mapped Aliases to PowerShell cmdlets, when you type an Alias in a PowerShell window or use an Alias in scripting, PowerShell will know to execute the cmdlet that’s mapped to the Alias.

Are PowerShell Aliases Saved?

You may be wondering if Powershell Aliases are saved indefinitely. By default, PowerShell Aliases are not saved if you close a PowerShell window session. For example, if you create multiple 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 retrieve the list of all existing PowerShell 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

Using the above method will ensure PowerShell Aliases remain active on the system but you may also find that you need to export and import PowerShell Aliases. This is useful when reinstalling a Windows Operating System or if you encounter issues with the PowerShell engine. To export all PowerShell Aliases from a Windows PowerShell profile, execute the following PowerShell command:

Export-Alias C:BackupPowerShellAliases.TXT

The above command exports all Aliases in a comma-separated values list to the “C:BackupPowerShellAliases.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:BackupPowerShellAliases.TXT –As “Script”

Importing PowerShell Aliases from a file is easy. Just execute this PowerShell command to start the Alias import process:

Import-Alias C:BackupPowerShellAliases.TXT

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 has multiple applications. You will need to set PowerShell context to “alias:” as shown in the following command:

Remove-Item Alias:Disp

If you wish to remove all Aliases from the PowerShell profile, simply execute a “Remove-Item Alias:*” command.

Potential Aliasing Issues

Using Powershell Aliases has its benefits but there are also some potential aliasing issues to be aware of:

  • Avoid adding too many aliases to your profile to prevent confusion. If you add too many aliases, then you run into the same problem you had previously of not being able to remember all of your most-used cmdlets.
  • Aliasing long, multi-command strings will cause the command to fail. Reserve your PowerShell aliases for only single commands or cmdlets.

Latest Posts

Data Center Management Training & Certifications

For System Administrators looking for ways to bolster their skills or to get ahead of the competition when job hunting, data center management training...

VMware Player Bridged Network

When you install a Workstation Player, a VMware Player bridged network is automatically set up for you. However, you may run into issues connecting...

ManageEngine OpManager Plus Pricing & Review

Server management is a critical component of maintaining a healthy and efficient IT infrastructure. This requires System administrators to continuously manage physical server hardware,...

Analyzing Web Server Logs

Modern organizations track and log data for virtually all business processes, which is why web server log analysis tools are vital for effectively using...

Harnessing the Power of PowerShell Aliases

Creating aliases for PowerShell cmdlets can be a valuable tool for saving you time and headaches over remembering the names of commands. PowerShell was...

Related Stories