Export (0) Print
Expand All
33 out of 41 rated this helpful - Rate this topic

Windows PowerShell Aliases

Windows PowerShell Owner's Manual


This is your guide to getting started with Windows PowerShell. Read through these pages to get familiar with Windows PowerShell, and soon you’ll be driving around like a pro.

On This Page

Windows PowerShell Aliases
Alias Defined
Built-In Aliases
Finding Aliases
Creating Aliases
Keeping Aliases Around
Alias Restrictions
Alias Wrap-Up


Windows PowerShell Aliases

Samuel Clemens. Archibald Leach. Ramón Gerardo Antonio Estévez. William Sydney Porter. Scripting Son. 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 a given name of 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 that, the Microsoft lawyers will be very unhappy: it was a legal name change. 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.

Alias Defined

Within PowerShell, an alias is another name assigned to a cmdlet, function, script, executable file, 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 it, 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 following 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 1.0 ships with about 100 built-in aliases. You can find these aliases with the Get-Alias cmdlet:



PS C:\scripts> get-alias

CommandType     Name         Definition
-----------     ----         ----------
Alias           ac           Add-Content
Alias           asnp         Add-PSSnapin
Alias           clc          Clear-Content
Alias           cli          Clear-Item
Alias           clp          Clear-ItemProperty
Alias           clv          Clear-Variable
Alias           cpi          Copy-Item
…


All the built-in aliases are simply short versions of cmdlet names. For example, the alias for Get-Alias is gal:



PS C:\scripts> gal

CommandType     Name         Definition
-----------     ----         ----------
Alias           ac           Add-Content
Alias           asnp         Add-PSSnapin
Alias           clc          Clear-Content
Alias           cli          Clear-Item
Alias           clp          Clear-ItemProperty
Alias           clv          Clear-Variable
Alias           cpi          Copy-Item
…


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 5 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:\Scripts\*.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’d use to list the items in the current directory:

Shell

Command

MS-DOS

Dir

UNIX

Ls

Windows PowerShell

Get-ChildItem


Sure, the PowerShell command seems 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. How about 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 the aliases associated with a given cmdlet (or definition). Well, that’s a little more complicated:



get-alias | where-object {$_.Definition -match "Get-Childitem"}


We told you it was more complicated. There isn’t a parameter to Get-Alias that, given a definition, will return the aliases. However, you can get all the aliases, then pipe them to the Where-Object cmdlet. We use the -match operator to find all instances of the returned Definitions that match the string Get-ChildItem. Here’s what we get back:



CommandType     Name         Definition
-----------     ----         ----------
Alias           gci          Get-ChildItem
Alias           ls           Get-ChildItem
Alias           dir          Get-ChildItem


More complicated, but still not bad, right?

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 assign 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

Wednesday, May 28, 2008 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:



PS C:\scripts> d

Wednesday, May 28, 2008 3:49:51 PM


The cmdlet works exactly the same; we just use an alias to reference it. For example, you can pass parameters to the alias in the same way you’d pass them to the cmdlet:



PS C:\scripts> d -displayHint date

Wednesday, May 28, 2008


As you can see, we used the displayHint parameter of the Get-Date cmdlet to display only the date portion of the date/time value, but we used that parameter with the d alias.

You can also alias functions. Here’s a function that finds the default printer on the local computer:



function FindDefaultPrinter
{
    Get-WMIObject -query "Select * From Win32_Printer Where Default = TRUE"
}


To run this function, simply type the name of the function, FindDefaultPrinter, at the command prompt:



PS C:\scripts> FindDefaultPrinter

Location      : USA/REDMOND, WA/42/FLOOR4/4032
Name          : \\PRINTER1\HP LaserJet
PrinterState  : 0
PrinterStatus : 3
ShareName     : HP LaserJet
SystemName    : \\PRINTER1


What’s that? FindDefaultPrinter is a big long name you don’t want to type in every time you want to check for the default printer? Okay, how about this:



PS C:\scripts> dp

Location      : USA/REDMOND, WA/42/FLOOR4/4032
Name          : \\PRINTER1\HP LaserJet
PrinterState  : 0
PrinterStatus : 3
ShareName     : HP LaserJet
SystemName    : \\PRINTER1


Will that work? Well, no, not unless you do this:



Set-Alias dp FindDefaultPrinter


Now it will work.

You can also alias executables. Want to start Microsoft Excel from the command prompt? Why would you ever want to do that? Oh well, that’s none of our business. But if you do, you can do it like this (assuming you have the default installation for Excel 2007):



."C:\Program Files\Microsoft Office\Office12\Excel.exe"


Or you can simply alias the command:



Set-Alias excel "C:\Program Files\Microsoft Office\Office12\Excel.exe"


Now all you have to type is this:



excel


It doesn’t get much easier than that.

Keeping Aliases Around

You’ve now spent a significant amount of time setting up all your aliases. Okay, maybe “significant” is exaggerating a little, but nevertheless you did spend some time on this. All system administrators know exactly how valuable time is and how little we have to waste, which is why we set up aliases in the first place. Now suppose you shut down Windows PowerShell, move on to some other tasks, and later restart PowerShell. Hmmm, let’s see what day it is today:



PS C:\scripts> d
The term 'd' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:1
+ d <<<<


Wait a minute - didn’t we just set the alias d to represent the cmdlet Get-Date? Yes, as a matter of fact we did. But then we shut down Windows PowerShell. Big mistake. Don’t ever, ever, shut down Windows PowerShell.

Well, not unless you save your aliases first.

There are two ways to do this. The first is to export your aliases to a file. This is much easier than it might sound. That’s because PowerShell makes it easy by providing the Export-Alias cmdlet:



Export-Alias MyAliases.txt


You need to pass Export-Alias only one parameter (although there are more, which we’ll talk about in a moment): the name of the text file that you want to save your aliases to.

Note: If you don’t want the file saved to the current path, you must specify the full path to the file:



Export-Alias C:\Files\MyAliases.txt



This command will export all aliases - user-defined and built-in - to the given file. Keep that in mind, because that could be a bit of a nuisance. We’ll show you that in a moment, too. But first, let’s talk about what we need to do with these exported aliases.

As we mentioned, when you close your PowerShell session all the aliases you defined will disappear. That means that the next time you open PowerShell, you’ll need to redefine all your aliases. However, if you exported them to a file first, you can simply import them back into your session and all your aliases will be back. You do that with the Import-Alias cmdlet:



Import-Alias MyAliases.txt


Simply pass the name of the file that contains your exported aliases (including the full path if the file isn’t in the current directory) to the Import-Alias cmdlet. Now you can use any of the aliases you had defined before the import.

What’s that? You tried this and got a big long list of errors? A list like this:



Import-Alias : Alias not allowed because an alias with the name 'ac' already exists.
At line:1 char:13
+ Import-Alias  <<<< MyAliases.txt
Import-Alias : Alias not allowed because an alias with the name 'asnp' already exists.
At line:1 char:13
+ Import-Alias  <<<< MyAliases.txt
Import-Alias : Alias not allowed because an alias with the name 'clc' already exists.
At line:1 char:13
+ Import-Alias  <<<< MyAliases.txt
…


First of all, don’t worry: your aliases are still there, and if you try one you’ll see that the import worked and your aliases will operate just fine. So why all the errors? Remember when we said Export-Alias will export all aliases, user-defined and built-in? Well, the user-defined aliases were wiped out when you closed PowerShell, but the built-in aliases weren’t. That means that when you run Import-Alias, all those built-in aliases that were exported are trying to be re-imported, but they already exist. Make sense? So, on the one hand these errors don’t hurt anything: they’re simply informing you that you can’t import a built-in alias because it already exists. On the other hand, who wants to see all those error messages every time they import the file? Not us, that’s for sure. But don’t worry, there’s something you can do about it.

One simple thing you can do is edit the alias file after you export it. Your exported file will look something like this:



# Alias File
# Exported by : kenmyer
# Date/Time : Wednesday, May 28, 2008 5:00:33 PM
# Machine : ATL-FS-001
"ac","Add-Content","","ReadOnly, AllScope"
"asnp","Add-PSSnapin","","ReadOnly, AllScope"
"clc","Clear-Content","","ReadOnly, AllScope"
"cli","Clear-Item","","ReadOnly, AllScope"
"clp","Clear-ItemProperty","","ReadOnly, AllScope"
"clv","Clear-Variable","","ReadOnly, AllScope"
…


Simply delete all the references to built-in aliases. Don’t worry, this isn’t hard to do: all the built-in aliases are listed first; your custom aliases are all at the bottom of the file:



…
"set","Set-Variable","","AllScope"
"type","Get-Content","","AllScope"
"d","get-date","","None"
"dp","FindDefaultPrinter","","None"
"excel","C:\Program Files\Microsoft Office\Office12\Excel.exe","","None"


See those last three, d, dp, and excel? Those are the aliases we added earlier. They’ll always be at the bottom.

The other thing you can do is add specific aliases to the file rather than doing a global export. You do this by passing a second parameter to Export-Alias, the name of the alias you want to export:



Export-Alias MyAliases.txt d


Here we’ve passed the alias d, which we set up for Get-Date. Here’s what our alias file looks like now:



# Alias File
# Exported by : kenmyer
# Date/Time : Wednesday, May 28, 2008 5:56:53 PM
# Machine : ATL-FS-001
"d","get-date","","None"


Here’s something important to know: every time you call Export-Alias, the current file is overwritten. That means that if you now call Export-Alias to export a different alias, d will be gone and will be replaced by the new alias. Like this:



Export-Alias MyAliases.txt dp




# Alias File
# Exported by : kenmyer
# Date/Time : Wednesday, May 28, 2008 5:59:29 PM
# Machine : ATL-FS-001
"dp","FindDefaultPrinter","","None"


But wait, here’s something even more important to know: you can pass another parameter to Export-Alias to make sure the new aliases are appended rather than overwritten:



Export-Alias MyAliases.txt d -append


Here’s what we have now:



# Alias File
# Exported by : kenmyer
# Date/Time : Wednesday, May 28, 2008 5:59:29 PM
# Machine : ATL-FS-001
"dp","FindDefaultPrinter","","None"
# Alias File
# Exported by : kenmyer
# Date/Time : Wednesday, May 28, 2008 6:00:52 PM
# Machine : ATL-FS-001
"d","get-date","","None"


Okay, this could still use some cleanup, but you don’t have to clean it up. (And if you want to clean it up it’s easy enough to do.) Either way, you won’t get any more error messages.

Note that exporting and importing aliases isn’t just handy for keeping aliases from one session of PowerShell to the next, but you can also copy the exported file to different machines and import them into the instance of PowerShell on those machines. Nice, huh?

There’s one more thing you can do to keep your aliases from one PowerShell session to another. Simply add the Set-Alias commands to your Windows PowerShell profile and your aliases will be available every time you start PowerShell, without having to do any extra importing.

We won’t go into detail on profiles here; to learn more see the Owner’s Manual topic on Windows PowerShell Profiles. We’ll just show you quickly how simply it is to add the aliases to your profile.

Start by opening your profile in Notepad, like this:



notepad $profile


Now just enter your Set-Alias commands (and any necessary functions) into your profile:



Set-Alias d Get-Date

function FindDefaultPrinter
{
    Get-WMIObject -query "Select * From Win32_Printer Where Default = TRUE"
}

Set-Alias dp FindDefaultPrinter

Set-Alias excel "C:\Program Files\Microsoft Office\Office12\Excel.exe"


Save the profile, close it, close Windows PowerShell, then open PowerShell again. Try out any of the aliases we just saved - they’ll all work. Every time, no extra steps involved.

How easy was that?!

Alias Restrictions

Here a couple things you can’t do with aliases. One is that you can’t alias a cmdlet with specific parameters. For example, say you use this command a lot:



Set-Location C:\Scripts\old


Suppose you want to create an alias to this command. Let’s give it a try:



Set-Alias toOld (Set-Location C:\Scripts\old)


Will this work? Nope. Here’s what you get back:



Set-Alias : Cannot bind argument to parameter 'Value' because it is null.
At line:1 char:10
+ Set-Alias  <<<< toOld (Set-Location C:\Scripts\old)


If you want to do something like this, you need to put the cmdlet in a function, then alias the function:



function toOld{Set-Location C:\Scripts\Old}


Now you can either call the function toOld, or you can alias the function:



set-alias to toOld


Now typing either toOld or to will take you to the C:\Scripts\Old folder.

You also can’t alias a pipelined command. Try this:



Set-Alias as (Get-Alias | Sort-Object)


It didn’t work, did it? Again, you can put the command in a function and then set an alias, but you can’t directly alias a pipelined command.

Alias Wrap-Up

Oh, and in case you haven’t figured it out yet, the names we mentioned at the beginning of this article belong to Mark Twain, Cary Grant, Martin Sheen, O. Henry, and - oh wait, that last one isn’t an alias, his name really is Scripting Son. Go figure.

Windows PowerShell Owner’s Manual

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.