With more than 2400 commandlets, learning Windows Server automation language PowerShell can seem like an overwhelming task -- but in truth there's only one commandlet you really need to know. PowerShell co-creator Jeffrey Snover offers his top strategies for learning the system.
Snover shared these tips in his 'PowerShell Unplugged' presentation at TechEd North America 2013, which was one of the more impressive talks I've seen recently. Through a series of live on-the-fly demos, Snover emphasised the following points.
1. Use the get-help command
Snover's single biggest message was that there's nothing "shameful" about needing to check the help. "I use Get-Help all the time, and that's the power of PowerShell," he said. "People don't start with the help. They ought to start with the help."
get-help cmdletname cmdlet can grab basic details for any PowerShell instruction. Combine that with the related
get-PSDrive commandlets, and you can learn how almost anything works. There are also useful parameters for particular kinds of information:
get-help cmdletname -examples,
get-help cmdletname -details and
get-help cmdletname -full.
2. It's a verb-noun structure
"The first thing you want to understand about PowerShell is that it is command-oriented," Snover said. "You think about what you want, you type it, you get it. Everything is verb-noun based. We've restricted the verb set so you can learn those verbs."
"When you want to learn, you should focus on processes," Snover suggested. "Run the command and start trying things"
3. Use PowerShell ISE
You can type PowerShell commands directly into a command line window, or use the PowerShell ISE (Integrated Scripting Environment). The big advantage of ISE is that it uses IntelliSense, so commands auto-complete; once you've typed the first part of a command and a dash, only relevant parameters will show up.
ISE offers both a scripting window and a command-line interface; you can type commands in the scripting window and then hit F8 to run in the command line.
4. Pop help into a separate window
Worried that an extensive help document will scroll your current work off screen? Snover offers two tips. Firstly, try
get-help cmdletname -ShowWindow to pop code into a separate window. "This works fantastic when you have a big monitor or multiple monitors," he said.
5. Access help on the clipboard or browser
On a smaller screen, try using
get-help cmdletname |clip to copy the help to the clipboard, use Control-T to open a new tab and then paste the help there. Another tactic: check the help in your browser by using
get-help cmdletname |online
6. Update the help regularly
PowerShell doesn't ship with any help built-in, in large part because the help content is updated so regularly. "Online help is always going to have the most recent and most current help versions," Snover said.
Snover recommends regularly using
update-help to stay current. For the full shebang, use
update-help -Module * -Force -Verbose.
7. Don't try and memorise everything
"PowerShell is not about sitting there and memorising things," Snover said. "It's about figuring things out and exploring. That's the way you should learn PowerShell and continue to use it."
8. Learn the common aliases
That said, for commands you use frequently, learning the shorter aliases can save typing time. For instance,
get-process can be shortened to
gps. You can discover the aliases by using (natch)
9. Don't be afraid of objects or .NET
"The key innovation of PowerShell is objects, but that freaks a lot of admins out: ''you want me to be a programmer'," Snover said. "Don't be afraid; the use of objects dramatically simplifies your life." While you can include quite complex programming code in PowerShell (when enclosed in curly braces), it isn't essential to the platform.
A related fear is that PowerShell requires a detailed understanding of the daunting .NET framework. "A lot of people have the mistaken idea that PowerShell is based on .NET," Snover said. . "PowerShell is implemented in .Net but it's quite different."
10. Don't panic about mistakes
"I'm going to make mistakes and that's part of the process," Snover said. You can always test the impact of a command by adding
-whatif, which shows what would happen but doesn't actually execute. Alternatively, you can use
-confirm to be prompted to confirm each action as it happens.