Windows PowerShell Aliases
Published May 20 2019 02:11 PM 342 Views
Brass Contributor
First published on TECHNET on Jun 06, 2010

So, would you rather type something like Get-AuthenticodeSignature or would you rather type something like gas ? We had a feeling you’d say that.

S amuel Clemens. Archibald Leach. Ramón Gerardo Antonio Estévez. William Sydney Porter. The list goes on…. And exactly what is this a list of? Well, as you might have figured out it’s a list of the given names of people who decided they didn’t want to (or couldn’t) become famous with those names. Instead, they took on new names, or aliases, in order to be more easily identified and remembered. As it turns out, Windows PowerShell has decided to join the other celebrities in utilizing aliases. How so? Read on.

It’s true that Windows PowerShell, before it became Windows PowerShell, began life with the given name Monad. It then briefly tried out MSH (pronounced “mish”), and finally settled on the catchy, rolls-smoothly-off-the-tongue, Windows PowerShell. Given that lifecycle you might think of Windows PowerShell as an alias. The problem is that, if we call it an alias, the Microsoft lawyers will be very unhappy: it was a legal name change. And because we find it best not to make the lawyers unhappy, we won’t use the term “alias” to refer to Windows PowerShell. But we will use the term “alias” to talk about a certain renaming feature within PowerShell. These aliases serve the same purpose as the aliases we mentioned at the beginning of this article: they make certain things in Windows PowerShell easier to identify and remember. (Although in some cases, if you’re not careful, an alias can be pretty effective at hiding an identity.)

Aliases Defined

Within PowerShell, an alias is simply another name assigned to a cmdlet, function, script, executable, and so on. Just about anything you can run from the PowerShell command prompt can have an alias assigned to it. We’ll explain how this works and why you’d even care about this by first showing you some of the aliases built-in to PowerShell. After that we’ll show you how to make aliases of your own. And then, after we’ve covered that, we’ll show you how to make sure the aliases you create stick around from one PowerShell session to another.

Are you ready for all that? Don’t worry, this will be easy. Even easier than remembering who Norma Jeane Mortenson was.

Built-In Aliases

Windows PowerShell 2.0 ships with well over 100 built-in aliases. You can find these aliases with the Get-Alias cmdlet:

PS C:scripts> get-alias

CommandType Name Definition

----------- ---- ----------

Alias % ForEach-Object

Alias ? Where-Object

Alias ac Add-Content

Alias asnp Add-PSSnapIn

Alias cat Get-Content

Alias cd Set-Location

All the built-in aliases are simply short versions of cmdlet names. For example, the alias for G et- Al ias is gal :

PS C:scripts> gal

CommandType Name Definition

----------- ---- ----------

Alias % ForEach-Object

Alias ? Where-Object

Alias ac Add-Content

Alias asnp Add-PSSnapIn

Alias cat Get-Content

Alias cd Set-Location

Alias chdir Set-Location

Alias clc Clear-Content

Alias clear Clear-Host

You can use these aliases alone, like we just did, or anywhere within a command string where you’d use the cmdlet. For example, here’s a command that retrieves the cmdlet associated with the alias ac :

PS C:scripts> gal -name ac

CommandType Name Definition

----------- ---- ----------

Alias ac Add-Content

You can imagine how much typing this can save you in the long run. Just to demonstrate, here’s a command that retrieves the five smallest text files in the C: folder:

get-childitem C:*.txt | sort-object -property length | select-object -last 5

Here’s that same command using built-in aliases:

ls C:*.txt | sort -property length | select -last 5

That’s better.


Note . If you use shorthand for the parameters you can compact this even more:



ls C:*.txt | sort -p length | select -l 5



But keep in mind that parameter resolution is a completely different feature than aliases. We’re not going to talk about parameters here. If this just confused you, ignore this note and keep reading.


A lot of the aliases are built-in simply to give you a quicker way to access cmdlets. However, some of them are there to make sure you can do things such as navigate your way around the file system in the command window by using familiar commands. For example, here are the commands you can use to list the items in the current directory:





















Shell



Command



MS-DOS



Dir



Unix



Ls



Windows PowerShell



Get-ChildItem


Admittedly, PowerShell’s commands sometime seem a little cumbersome compared to the others. But with built-in aliases you can use any of these commands in PowerShell (in addition to the alias gci ) to retrieve the contents of a folder.

Finding Aliases

We already showed you how to get a list of all the aliases available (the Get-Alias cmdlet). Here’s how to get a list of aliases sorted by alias:

get-alias | sort-object

Want to sort by definition (the cmdlet, function, etc. that the alias is an alias for)? Okay:

get-alias | sort-object definition

How about this: let’s retrieve the definition associated with a given alias. We’ll try the cd alias:

PS C:scripts> get-alias cd

CommandType Name Definition

----------- ---- ----------

Alias cd Set-Location

This is all pretty simple, right? The next logical step would be to find all the aliases associated with a given cmdlet (or definition). That’s pretty easy too:

PS C:scripts> Get-Alias -Definition Set-Location

CommandType Name Definition

----------- ---- ----------

Alias cd Set-Location

Alias chdir Set-Location

Alias sl Set-Location

Creating Aliases

Okay, this is all very nice, but what if you don’t like the built-in aliases? Or what if you want to set an alias for a function that you’ve created? As it turns out, this is no problem at all. Simply use the Set-Alias cmdlet. Suppose you want to retrieve the current date. You could do that with the Get-Date cmdlet:

PS C:scripts> Get-Date

Monday, June 7, 2010 3:47:56 PM

That’s simple enough, but let’s make it even simpler – let’s set an alias for Get-Date:

Set-Alias d Get-Date

Logically enough, we use the Set-Alias cmdlet to create an alias. We pass two parameters to this cmdlet: the new alias ( d ) and the cmdlet being aliased (Get-Date). Let’s try it out:


Version history
Last update:
‎May 20 2019 02:11 PM
Updated by: