Windows PowerShell Get-Alias
Introduction to Windows PowerShell Get-Alias
PowerShell 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
I 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.
In the old days I listed PowerShell's aliases with this command:
# Research PowerShell Alias
Then I discovered that alias has its own cmdlet:
Note 1: The secret is employ the -Definition parameter.
Note 2: The benefit of the PowerShell Get-Alias is that you can investigate more properties by appending | Get-Member, thus:
# Research Get-Alias Properties
Note 3: 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 4: To list only the DOS and UNIX commands change -Match to -NotMatch
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.
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.
As you check this list see if you can detect two types of PowerShell Alias cmdlets, 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).