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.”
The get-help cmdletname
cmdlet can grab basic details for any PowerShell instruction. Combine that with the related get-member
, get-process
, get-command
and 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) get-alias
.
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.
Visit Lifehacker’s World of Servers Newsroom for all the latest news from TechEd North America 2013. And don’t forget: TechEd is coming to Australia in September. Click here for more information.
Comments
2 responses to “Top 10 Tactics For Learning PowerShell”
The object-oriented-ness of PowerShell is great. It’s easy to write complex functions and use all the standard programming structures from many common languages (variables, arrays, if statements, for loops, etc.).
I’ve been using it a lot lately with SCVMM 2012 SP1 (not sure if the pre tags will work):
$runningVmArray = @()
foreach ($vm in Get-VM) {
if ($vm.Status -eq “Running”) {
$runningVmArray += $vm;
}
}
echo $runningVmArray | Sort Name | FT Name,Status
I use powershell daily — To configure VM rollouts, software deployment/configuration, and sharepoint deployment/administration, and I love it.
Most of these steps are spot on as well. Though to me, the highlights are Get-Help, using ISE, and using .Net objects.
I love just going ‘damn, that 1 line of powershell is 3-4 in c#, but these 2 lines of c# code are easier than powershell’ and mixing it all up in the one script.
Also makes it a lot easier to share sample code with clients/partners. no-one trusts .net code they get give,n, but a .ps1 file? They’ll run it without a second thought.
Another one I’d add in there is Get-Command. This cmdlet shows all of the cmdlets available in all of your loaded modules. Along with Get-Help as you mentioned it’s a great discovery cmdlet that allows a beginner to get a good feel for what they’re dealing with.
– Adam
adamtheautomator.com