PowerShell


Windows PowerShell Aliases

Introduction to Windows PowerShell Aliases

PowerShell's built-in Aliases will help you make the transition from another command shells.  For example, in DOS the 'cd' command means change directory, in PowerShell there is an Alias which maps the native set-location to DOS's cd.  The result is that in PowerShell you can type cd and it works just as it did in DOS. 

There is a second family of PowerShell Aliases, these commands are designed simply to reduce your keystrokes, for example, instead of typing thirteen keystrokes for get-Childitem, just type three letters: gci.

PowerShell Alias Topics

Check Aliases with get-alias

Let us start with the master command which lists PowerShell's built-in aliases.  Launch PowerShell and type:
get-alias.

You can also filter get-alias, for example, [a-g]* lists all the aliases beginning with letters 'a' through 'g'.  Incidentally, this simple example demonstrates how PowerShell employs wildcards, and also alerts you to the significance of different types of brackets.

As ever, you can refer to help, for example type: help get-alias.  There is also: help new-alias, or even help delete-alias.  I have deliberately not emphasised creating your own aliases.  My reasoning is this, any aliases you create will not work if you send scripts containing such aliases to other people.  This could cause confusion and thus is best avoided especially if you are just starting to learn PowerShell.

 List of PowerShell's Built-in Aliases

As you check this list see if you can detect two types of PowerShell Aliases, those convenience Aliases that simply save key strokes, for example, gci (get-Childitem) and those Aliases that help people transition from other languages, for example, cd (set-location).

Alias Definition Alias Definition
ac add-content kill stop-process
cat get-content lp out-printer
cd set-location ls get-Childitem
chdir set-location mi move-Item
clc clear-content mount new-drive
clear clear-host move move-Item
cli clear-Item mp move-property
clp clear-property mv move-Item
cls clear-host nal new-alias
clv clear-variable ndr new-drive
copy copy-Item ni new-Item
cp copy-Item nv new-variable
cpi copy-Item oh out-host
cpp copy-property popd pop-location
cvpa convert-path ps get-Process
del remove-Item pushd push-location
dir get-Childitem pwd get-location
echo write-object r invoke-history
epal export-alias rd remove-Item
epcsv export-csv rdr remove-drive
erase remove-Item ren rename-Item
fc format-custom ri remove-Item
fl format-list rm remove-Item
foreach foreach-object rmdir remove-Item
ft format-Table rni rename-Item
fw format-wide rnp rename-property
gal get-alias rp remove-property
gc get-content rv remove-variable
gci get-Childitem rvpa resolve-path
gcm get-command sal set-alias
gdr Get-psdrive sasv start-service
ghy get-history sc set-content
gi get-Item select select-object
gl get-location set set-variable
gm get-Member si set-Item
gp get-property sl set-location
gps get-Process sleep start-sleep
group group-Object sort sort-Object
gsv get-service sp set-property
gu get-unique spps stop-process
gv get-variable spsv stop-service
h get-history sv set-variable
history get-history type get-content
icm invoke-command where where-object
ihy invoke-history    
ii invoke-Item    
ilal initialize-alias    
ipal import-alias    
ipcsv import-csv    
  ˚

Create your own PowerShell Alias

The command to add your own alias is: set-alias.  You follow this command with your choice of aliasname and then complete the instruction by specifying an existing PowerShell verb-noun, for example:

new-alias xcopy copy-Item
or
set-alias xcopy copy-Item works just as well.

The new alias is called xcopy and what it does is the equivalent of copy-Item.  To double check type:

get-alias xcopy

N.B. Don't go mad with aliases, stick to one verb and one noun. I tried this:
set-alias eventvwr get-Eventlog application, however it did not work as I had hoped - too many arguments.
set-alias eventvwr  is ok, but rather disappointing because it prompts you for the name of the log.

Function - An alternative to Alias
If you really need a more complex set of commands then consider creating a function, which you can then save to the Function 'drive', investigate with:  get-psdrive or get-psdrive function.

Sequencing - Aliases come first
Suppose you have an Alias called 'eventvwr' and also a cmdlet with the same name 'eventvwr.ps1', but with different instructions.  What would happen is the Alias would be processed first.  In fact PowerShell checks the Aliases before it looks for functions or cmdlets.

How to permanently save your Alias

If you just create an alias at the command prompt it is desperately disappointing that PowerShell does not remember your aliases the next time you logon.  Good news, you can add your aliases to profile.ps1, the benefit is they will now be available for each and every session.

My friend 'Barking' Eddie even has a file just for his aliases, he calls it profile_alias.ps1.  His trick is to call this dedicated file from within profile.ps1 with this line:

."C:\ Documents and Settings\EDDIE\My Documents\PSConfiguration\profile_alias.ps1"

If you try this at home, then substitute your username for EDDIE, or else it will not work.
N.B. That's not dirt on the screen or a Guy error, there really is a full stop or 'period' in front of ."C: \ ".

Some say Eddie is Barking mad, but actually he is from Barking in Essex, either way, he takes this linking idea further and creates a profile_function.ps1 file for his special functions.

Aliases in cmdlets?
Can you call an Alias from a cmdlet?  The answer is yes, why ever not, remember that cmdlets mimic keystrokes you type at in the Microsoft Shell.

Problems caused by custom PowerShell Aliases

My old friend 'Barking' Eddie has produced some great PowerShell scripts and at present they work fine.  However, Eddie has created a time bomb, the problem is that his cmdlets have so many bizarre aliases that no-one else can understand them.  Eddie has no scruples, he is hoping that the people who employ him today, will pay him big bucks to come out of his retirement and fix those cmdlets that no-one else can decipher.

The lesson is this, if you only have yourself to please, then create all the aliases that you want.  However, if others need to understand or troubleshoot your scripts, then filling them with aliases will only store up problems, which will return to haunt you.

Summary of PowerShell Aliases

As far as I can see, using Aliases in PowerShell has these three benefits:

  1. To reduce typing.
  2. To smooth the transition from cmd.exe or other scripting languages to PowerShell.
  3. To create your own shortcuts for commands that you use often.

See Also

Windows PowerShell Home  • Introduction  • Cmdlets  • Exchange 2007  • Profile.ps1  • $_.Pipeline

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.