Windows PowerShell AliasesIntroduction to Windows PowerShell AliasesPowerShell'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-aliasLet us start with the master command which lists PowerShell's built-in aliases. Launch PowerShell and type: 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 AliasesAs 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).
˚
Create your own PowerShell AliasThe 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 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: Function - An alternative to Alias Sequencing - Aliases come first How to permanently save your AliasIf 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. 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? Problems caused by custom PowerShell AliasesMy 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 AliasesAs far as I can see, using Aliases in PowerShell has these three benefits:
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. *
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||