Your First 5 Minutes with Windows PowerShell
Here's how to make your first PowerShell experience a good one right from the outset.
- By Jeffery Hicks
Unless you've been out of the office for the last year or stuck deep in the bowels of your data center, you should know that Windows PowerShell is here and here to stay. Whether you like it or not, this is the management tool you'll need to begin using. But this doesn't have to be a hair-pulling, teeth-rattling process. Mr. Roboto is here to help.
PowerShell is first and foremost an interactive management console: You give PowerShell some instructions, and wonderful things happen. So, let's say you've never seen a PowerShell prompt but you have it installed. Here's how you might spend your first five minutes.
The first thing to do is learn how to use PowerShell's extensive help system. At a prompt, type:
Use the space bar to page through the results. These are all topics that PowerShell can help you with. Pick one and ask for full help:
Help get-service -full
You will get syntax, parameter descriptions and complete working examples. Every cmdlet, PowerShell's command unit, is documented. Run this:
These are the topic files that provide more information about things such as the pipeline, functions and remoting. I know some of the names are long, but you only have to type enough of the name so that PowerShell knows what you're after:
This will display topic information about the Switch construct. Of course, a well-written reference book is helpful, but PowerShell's documentation and help system works well in a pinch.
PowerShell is an object-based shell, unlike the text-based shell of the cmd.exe and Unix/Linux shells. Even though you run a command and see text, what you're really seeing is an object. Try these commands in your second minute:
Get-date | Get-member
The first thing you typed is calling the Get-Date cmdlet, which writes the current date and time -- although the details are more complicated than that. The second expression takes the result of Get-Date and pipes or sends it to the Get-Member cmdlet. This cmdlet is designed to tell you everything about an object: in this case, it's a date-time object with a variety of properties. This is helpful because not every property is always displayed. At a prompt type:
These are the current processes on your computer. Run this command through Get-Member, and you'll see there are even more properties of a process object.
In your third minute, try navigating around PowerShell. You can use CD to change directories and drives. But PowerShell can do much more. To see all of PowerShell's drives, run this:
PowerShell uses different adapters to present different systems as a file system. Try these commands:
You're now looking at the registry on your machine. When finished exploring, change back to your C: drive.
A few minutes ago, you experienced your first pipelined expression.
PowerShell's cmdlets are designed for single-purpose tasks such as sorting and filtering. By sending objects from one cmdlet to the next, you can achieve some very remarkable results without writing a single script. You've already seen Get-Process in action. Try this:
get-process | sort workingset
-descending | select -first 10
With hardly any effort, you have a sorted list of the top 10 processes based on the workingset size. If you have a few moments left, take a look at the About_Pipelines help topic.
Fifth Minute-and Beyond
In your last minute, look at full help for these cmdlets: Where-Object, Select-Object, Out-File, Out-Printer and Get-WMIObject. These are cmdlets you'll be using frequently. From this point on, all you have to do is explore and try things out.
Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org.
Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide.
You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)