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
♣
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 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!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
Download your free copy of WMI Monitor
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.
Guy Recommends: SolarWinds Engineer's Toolset v10
This 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 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, discovery, diagnostic, and Cisco tools.
Download your fully functional trial copy of the Engineer's Toolset v10
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
• PowerShell Tutorials • Introduction •
Vista PowerShell •
Windows 7 PowerShell 2.0
• PowerShell Parameters •
-Online •
PowerShell Alias •
3 Key Commands
• Cmdlet scripts •
Windows
PowerShell •
PowerShell $_ Variable
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.
Download my ebook: Getting Started with PowerShell - only $9.25
You get 36 topics organized into these 3 sections: 1) Getting Started 2) Real-life tasks 3) Examples of Syntax.
In addition to the ebook, you get a PDF version of this Introduction to PowerShell ebook It runs to 120
pages of A4.
|