Ezine 198 – PowerShell’s Aliases

Ezine 198 – PowerShell’s Aliases

Understanding PowerShell’s aliases is a wonderful way of getting started with this command-line language.  For those who have already some PowerShell knowledge, a refresher on aliases will help you add shortcuts such as gm, where (?), sort and ft to your scripting vocabulary.

Topics for PowerShell’s Aliases

This ezine is now available at http://twitter.com/ComputerPerfGuy

 ♣

This Week’s Secret – Aliases

PowerShell has a whole family of Aliases.  One group of these aliases provides shorthand for the regular cmdlets, for example, gci is an alias for get-ChildItem, or gwim for get-WmiObject.  However, this week I am concentrating on the aliases 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.

This Week’s Mission – Abandon DOS

This week 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 aliases is how to 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.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

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 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.

Download your free copy of WMI Monitor

Check Out PowerShell’s Aliases

In the old days I listed PowerShell’s aliases with this command:

get-Command -commandType alias

Then I discovered that alias has its own cmdlet:

get-Alias
# To execute each of the following lines remove their #
# get-Alias | get-Member
# get-Help get-Alias -full
# get-Alias -definition get-ChildItem

Note 1: The benefit of get-Alias is that you can investigate more properties by appending | get-Member, thus:
get-Alias | get-Member.

Note 2: get-Help get-Alias (Or use the alias ‘help’ and try plain: ‘help get-Alias’).  Now we have extra information try:
get-alias -definition Get-ChildItem

Filter for only built-in Aliases (They have the ReadOnly property)

get-alias | where-object {$_.Options -match "ReadOnly"}

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:
get-alias | where-object {$_.definition -match "Object"}

Summary, Use PowerShell Rather than DOS

I would like to persuade you to change from running simple instructions such as Ipconfig or Ping at the DOS box command line (cmd.exe) and run them in PowerShell instead.  I will be frank with you here, there is no real benefit to using PowerShell instead of DOS, however, what it does us give you practice with PowerShell.

If you go for this cost-nothing approach to learning PowerShell you will soon discover two useful threads, firstly, PowerShell has aliases for common operating instructions such as dir, copy and ren.  Secondly, PowerShell allows you to run most of the operating system’s built-in commands such as Ipconfig and Shutdown, just as you would in a DOS box.  More interactive commands will run if you precede them with Start-Process, thus start-Process NetSh.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

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

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials   • Introduction   • PowerShell 3 New Aliases   • -Online   • 3 Key Commands

Top 10 PowerShell Aliases   • PowerShell Alias   • PowerShell $_ Variable   • Free CSV Import Tool

PowerShell Parameters   • Cmdlet scripts   • Vista PowerShell   • Windows 8 PowerShell 3.0

Please email me 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.