Windows PowerShell Aliases

style=” padding-left: 1px; padding-right: 1px; padding-top: 1px” width=”112″> 

  ˚

Create your own PowerShell Alias

The command to add your own alias is: set-alias.  You follow with the aliasname and then the 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
set-alias eventvwr get-eventlog application, however this 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 functions which you can then save to the Function drive, investigate with:  get-psdrive or get-psdrive function.

Sequencing
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 the cmdlets have so many bizarre aliases that no-one else can understand them.  Eddie has no scruples, he is banking on people who employ him today, will pay big bucks to call him out of his retirement to fix his scripts that no-one else can decipher.

The lesson is if you only have yourself to please, then use 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.

Summary of PowerShell Aliases

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

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

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.