Site icon Think PowerShell

PowerShell Aliases: Save Time But Use Wisely

PowerShell Get-Alias result screenshot

When typing interactive PowerShell commands, some cmdlets can be lengthy. Save yourself some time by making use of a PowerShell cmdlet’s Alias.

PowerShell is designed for readability

A big PowerShell strength is its readability. Cmdlets follow a standard Verb-Noun format and avoid any acronyms that aren’t considered common knowledge. By simply looking at a command, you can quickly understand what it is intended to do.

Readability is very important when you are creating a finished script that will be reviewed, used, and updated by others. But what about when you are simply performing some interactive work from a PowerShell prompt? You can tab autocomplete a cmdlet, but that still requires a minimum amount of typing and you may need to tab through a few cmdlets before you get to the one you want. Save yourself some time and typing by using a cmdlet’s Alias.

PS C:\Users\aaron> Get-Service -DisplayName 'Windows Update'

Status     Name        DisplayName 
------     ----        ----------- 
Stopped    wuauserv    Windows Update

What’s a PowerShell Alias?

PowerShell Aliases are shortcuts or shortcode for a PowerShell cmdlet. Instead of typing an entire cmdlet name, you can type a predetermined combination of letters that PowerShell will correctly translate to the given cmdlet.

For example, instead of typing Get-Service, I can type gsv:

PS C:\Users\aaron> gsv -DisplayName 'Windows Update'

Status    Name         DisplayName 
------    ----         ----------- 
Stopped   wuauserv     Windows Update

And that is pretty much all there is to an Alias. PowerShell comes with a bunch of built-in Aliases, which you can see with the cmdlet Get-Alias. If you don’t use any parameters, it will list all of the Aliases available on the computer.

If you want to see if there is an Alias for a specific cmdlet, you can use the -Definition parameter along with the name of the cmdlet (below). Note that there can be more than one Alias for a cmdlet, but never more than one cmdlet tied to an Alias.

PS C:\Users\aaron> Get-Alias -Definition Get-Process

CommandType    Name    Version    Source 
-----------    ----    -------    ------ 
Alias gps ->   Get-Process 
Alias ps ->    Get-Process

Create a custom PowerShell Alias

Let’s say you drop into a PowerShell prompt and are getting ready to type the same cmdlet a bunch of times (NOTE: This shouldn’t really ever be a scenario; use piping, loops or a script!). Unfortunately, you find out there is no built-in Alias for the cmdlet (below).

PS C:\Users\aaron> Get-Alias -Definition Get-ControlPanelItem
Get-Alias : This command cannot find a matching alias because an alias with the definition 'Get-ControlPanelItem' does not exist.
At line:1 char:1
+ Get-Alias -Definition Get-ControlPanelItem
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : ObjectNotFound: (Get-ControlPanelItem:String) [Get-Alias], ItemNotFoundException
 + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand

In this situation, you can create your own custom Alias for the cmdlet using the Set-Alias cmdlet. This will create a session-specific Alias that you can use.

PS C:\Users\aaron> Set-Alias -Name gcpi -Value Get-ControlPanelItem

PS C:\Users\aaron> gcpi -Name "Windows Firewall"

Name                 CanonicalName                 Category                 Description
----                 -------------                 --------                 -----------
Windows Firewall     Microsoft.WindowsFirewall     {System and Security}    Set firewall security opti...

If you want to have this Alias persist for your user on the computer your using after your PowerShell session ends, you can add the Set-Alias command to your PowerShell profile (which is a topic for another day).

Built-in Aliases: gal, sal

It wouldn’t be right if the Alias cmdlets didn’t have their own aliases!

gal -> Get-Alias

PS C:\Users\aaron> gal -Definition Get-Alias

CommandType Name
----------- ----
Alias gal -> Get-Alias

sal -> Set-Alias

PS C:\Users\aaron> sal -Name gcpi -Value Get-ControlPanelItem

 

Recommended practice for using PowerShell Aliases

Feel free to use PowerShell Aliases when you are working interactively in the PowerShell console. And that’s it.

When writing scripts that are going to be reviewed, used, and updated by others, prioritize readability and use the full cmdlet name.

Reference

Exit mobile version