style=” padding-left: 1px; padding-right: 1px; padding-top: 1px” width=”112″>
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
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:
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.
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.
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.
As far as I can see, using Aliases has three benefits:
- To reduce typing.
- To smooth the transition from cmd.exe to PowerShell.
- 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
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links.