Prof. Powershell
I Can Type That Command In 10 Characters
Make PowerShell do all the typing. How? By taking advantage of aliases, of course.
- By Jeffery Hicks
- 09/25/2012
One of the most common complaints about PowerShell is the amount of typing. I get it. After decades of pointing and clicking, spending any significant amount of time at the keyboard can be quite tedious. But your PowerShell experience doesn't have to be this way. You just need to make PowerShell do the work for you, especially if you are using PowerShell interactively at a console prompt, which is what I want to discuss.
The first step you should do is to take advantage of aliases. The PowerShell team knew that they needed to provide a transition for people. Thus instead of trying to figure out that you should use Get-ChildItem to do a directory listing, you can use DIR or LS. Now, those are not the same commands you may be used to. But you can type DIR C:\Windows and behind the scenes PowerShell will translate DIR into Get-ChildItem.
Aliases were also developed to improve your efficiency at the keyboard. Typing Get-WMIObject can be cumbersome. But using gwmi is pretty easy. You can discover all the aliases in your PowerShell session with the Get-Alias cmdlet:
PS C:\> Get-Alias
Get-Alias even has an alias of gal. There's no way around a bit of a learning curve, but if you want to see if there is an alias for a given command, use this cmdlet with the - Definition parameter:
PS C:\> gal -Definition get-service
If you run this command you should see an alias of gsv. It doesn't take too long to pick up on the aliases you will use on a regular basis. And of course, you can always create your own which is a topic for another time.
The next PowerShell perk is that you don't always have to use parameter names. Some parameter names are positional, meaning it is assumed that the first thing PowerShell sees will be assigned to that parameter. This means that instead of typing a command like this:
PS C:\> Get-WmiObject -Class Win32_Logicaldisk
I can simplify it to this:
PS C:\> gwmi win32_logicaldisk
For parameters that aren't positional, you only have to type enough of the parameter name so that PowerShell can tell what parameter you mean. Compare this:
PS C:\> Get-WmiObject -Class Win32_LogicalDisk -filter "drivetype=3" -computername (get-content - path "c:\work\computers.txt") | Format-Table -GroupBy SystemName -Property DeviceID,Size,Freespace
to this:
PS C:\> gwmi Win32_LogicalDisk -f "drivetype=3" -co (gc "c:\work\computers.txt") | ft -G SystemName DeviceID,Size,Freespace
Which would you rather type? Although, PowerShell has tab completion for cmdlet names and parameters so it isn't necessarily that hard to type out a command.
One important final note: everything I've discussed assumes you are working at a console prompt. If you are typing a command like
PS C:\> ps | ? {$_.ws -ge 50mb} | select Name -u
I'm assuming you know what it means and efficiency at the console is likely more important than clarity. But, when writing a script, clarity is more important since you only have to type the command once. Plus, tools like the PowerShell ISE offer Intellisense and tab completion ease the burden. So in a script, you would use the full cmdlet and parameter names.
PS C:\> Get-Process | Where-Object - filterscript {$_.workingset -ge 50mb} | Select-Object -Property Name -Unique
Yes, this is more to type. But you only do it once and it is meaningful.
So by all means, use all the keyboard and PowerShell shortcuts you can when working at the console. In fact, challenge yourself to see how short you can go. I bet you'll even learn a little more PowerShell along the way.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.