|
Guy recommends: This CG will put you in charge of controlling changes to network routers and other SNMP devices. Download your free Config Generator |
Windows PowerShell Get-AliasIntroduction to Windows PowerShell Get-AliasPowerShell has a whole family of Aliases. One group of these aliases provides shorthand for the regular cmdlets, instead of typing thirteen keystrokes for Get-Childitem, just use a PowerShell alias and type the three letters: gci. However, I am going to concentrate on alias cmdlets which are designed to help people migrate from other languages. Thanks to aliases, those who know DOS can still use dir, and those who are familiar with UNIX can still use ls in PowerShell. These old commands work because PowerShell has created an internal link so that when you type dir it translates to the cmdlet Get-ChildItem. PowerShell Alias Topics
♣ A Mission For PowerShell Aliases - Abandon DOSI have a tough mission, to persuade people to abandon the DOS command-line and switch to PowerShell. There are three threads to this mission. 1) Have faith that an alias such as cd will change directory in PowerShell just as it did in DOS. 2) Realize that built-in operating system commands such as Ping, Ipconfig and Shutdown work in PowerShell 98% as well as they did with cmd.exe. Consequently, there is no need to use that DOS box, just use the PowerShell command-line instead. I understand that this is a leap of faith, and to begin with may take you out of your comfort zone. 3) The third aspect of the PowerShell alias cmdlets is how they deal with native operating system commands that need user input. For this task we can employ start-Process, or the versatile PowerShell cmdlet new-Object with its -comObject parameter. This week I just want to make you aware that interactive command-line utilities, such as NetSh will work in PowerShell, however they need techniques outside the scope of this article. In conclusion, my idea is that you will learn PowerShell faster if you use it for familiar DOS tasks. Then gradually, it will become easier and more natural to start incorporating more and more pure PowerShell commands into your working habits. Check Out PowerShell's Get-AliasIn the old days I listed PowerShell's aliases with this command: # Research PowerShell Alias Then I discovered that alias has its own cmdlet: Get-Alias Note 1: The benefit of the PowerShell Get-Alias is that you can investigate more properties by appending | Get-Member, thus: # Research Get-Alias Properties Note 2: Get-Help Get-Alias (Or use the alias 'help' and
try plain: 'help Get-Alias'). Now we have extra information try: Filter for only built-in Aliases (They have the ReadOnly property) #PowerShell Alias Example Note 1: To list only the DOS and UNIX commands change -match to -notMatch Note 2: 'Where' is an alias for the cmdlet
'where-Object'. Check for more 'object' aliases with: More Filters for the PowerShell Alias Family 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.
Guy Recommends: A Free Trial of the Orion Network Performance Monitor
(NPM) v10
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 |
The command to add your own PowerShell 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:
# PowerShell create alias
Clear-Host
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.

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems. Guy says it helps me monitor what's occurring on the network, and the tools teach me more about how the system itself operates.
There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools. Download your copy of the Engineer's Toolset v 10
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.
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.
As far as I can see, using a PowerShell Alias has these three benefits:
• PowerShell Tutorials • Introduction • Vista PowerShell • Windows 7 PowerShell 2.0
• PowerShell Parameters • -Online • PowerShell Alias • 3 Key Commands • Cmdlet scripts
Please write in if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Download my ebook:
|
*
|
|
Guy
Recommends: WMI Monitor and Its Free!
|
|
Home Copyright © 1999-2010 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |