Polish Your Shell
Learning PowerShell is simple once you understand these basic cmdlets.
With the impending release of Windows 7 and Windows PowerShell version 2.0, it will become more difficult to ignore PowerShell. You shouldn't ignore it. PowerShell is definitely easier to learn than you think.
PowerShell ships with a very robust help system. Every cmdlet -- the basic PowerShell command you'll run -- includes extensive help. You can use either the Get-Help cmdlet or the help command, which accepts wild cards. Both of these options make getting help even easier. Suppose you need to do something with a service but don't know the commands. You can ask for help by typing "help" and what you want help with.
PS C:\> help *service*
You should get a list of cmdlets. Pick Get-Service:
PS C:\> help get-service
PowerShell will display a summary, syntax and notes. But it gets better. Need more information? Ask PowerShell. Try these commands:
PS C:\> help -get-service -detailedPS C:\> help -get-service -full
The -detailed and -full parameters provide increasing levels of detail.
Discovering information about an object is very easy with the Get-Member cmdlet. Pipe any object to Get-Member, and PowerShell will tell you everything it knows about the object:
PS C:\> get-process | get-member
The first thing you'll see is the object type, which in this example is a System.Diagnostics.Process. This is helpful in the event that you need to research the object's properties or methods further. Notice all the properties and methods? Don't worry too much about methods right now, because you'll use cmdlets to manage most things. But the property list is important. When you run a cmdlet, PowerShell presents a default view of the objects in the pipeline. More often than not, more properties exist for you to use than are displayed in this view. Use Get-Member whenever you want to learn what objects are coming out of the end of the pipeline.
The last cmdlet that will help you teach yourself about PowerShell is Select-Object, which has an alias of Select. This cmdlet can be used to return the first or last X number of objects from the pipeline:
PS C:\> dir $env:temp | sort length -descending | Select -first 3
This expression will list all files in the %TEMP% directory -- sorted by size in descending order -- and select the first three objects. Were you expecting size? Pipe a file object to Get-Member to see for yourself:
PS C:\> dir c:\windows\notepad.exe | get-member
There's no size property, but you should have seen length in the command output. Or, if you aren't sure, use Select-Object with its other syntax and select the specific properties you want:
PS C:\> dir $env:temp | select Name,Length
All you need to do is specify a comma-separated list of properties. But it gets better. I often want to know all the properties and their values for an object. This helps me discover how an object is designed. Once I know that information, I can write more effective PowerShell expressions:
PS C:\> get-eventlog -LogName System -Newest 1 | select *
Now I know all the properties for an eventlog object and their corresponding values, which makes it easier to develop more specific PowerShell expressions:
PS C:\> get-eventlog -LogName System | Sort Source | group Source | Sort Count -descending | Select Count,Name
If you run this expression, you'll get a list of all event sources sorted by number of events in descending order. Curious as to how I developed this expression? You can figure it out for yourself with everything I've shown you. Start with the Get-Eventlog cmdlet. Look at help. Pipe an expression to Get-Member or Select-Object. Add the next pipelined part, Sort Source, and repeat.
Learn how to use these basic cmdlets, and you'll be learning PowerShell without realizing it.
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)