Guest blog post from my operations friend Josh Boyles (I'll have to remind him to get a twitter account so you can let him know what you think of his post… especially since I know the comments on my blog at this moment are painful.)
Last time we gave a little bit of an introduction. Today we're getting right down to it. Time to get our hands dirty with a little powershell. And when I say get our hands dirty I mean we're going to be typing on keyboards, and those are disgusting.
I'm going to assume you're at least a little familiar with PowerShell. If not, go read the last post. In PowerShell you use what are called "commandlets." And what are those?
Commandlets are an Admins Best Friend
In this metaphor Marilyn Monroe is a SharePoint admin. Which is probably what she would've done if she hadn't made it big in the pictures.
Commandlets are the easiest way to get things done with PowerShell. Sure, you can program commands yourself and do everything the hard way, but commandlets are little programs that already do the things you most likely want to do. Think of it like the difference between assembly code and python. Sure, you could write how to add two numbers in assembly, but it would take way too much work and end up pretty ugly. With python, you just write 1 + 1 and hit enter.
There are over 100 Commandlets (or is it CommandLets? Microsoft has some weird obsession with capital middle letters) built into PowerShell itself, but the real power comes from the fact that most major Microsoft products have snapins or modules built by the product team. A snapin is a collection of commandlets specific to a certain product. A module is a little bit more than a snapin, but we won't get into that because in SharePoint 2010 snapins are still used.
So let's use the commandlet to import the snapin as our first example (that's called recursive learning … a method I just made up) -- (wait a second, I just went and looked it up just to be sure and, not only did I not invent it, recursive learning isn't even close to what I didn't invent). Where were we before all those parentheticals? That's right, importing a snapin using a commandlet. So, we open up PowerShell and, in the command line window that opens, type in:
PS C:\>add-pssnapin microsoft.sharepoint.powershell
Tap our shoes together three times, hit enter and … our PSSNAPINS are imported! All commandlets follow the same pattern you see here. The first word is a verb, then a hyphen, then a noun, like so:
Add-PSSnapin
You start with what is going to happen (in this case, we're ADDING something, so it starts with add) and end with a noun representing what will be acted upon (in this case, it's a pssnapin, or PowerShell SnapIn).
This format is INCREDIBLY useful when we try to search for commands, but we'll get into that in a second.
Back to our example, we're adding a pssnapin, and then the next bit is the pssnapin that's getting added here. The section following the command is where you put options, sometimes referred to as switches, arguments, parameters, etc. Depends on which side of the tracks you come from.
Whatever you call them (and we will call them parameters from here on out), in powershell they are indicated by a dash, followed by the name of the parameter, then a space, then any input for that parameter (not all parameters need input). For example, if we wanted to get technical with our add-pssnapin, what we should've written was this:
PS C:\>add-pssnapin -name microsoft.sharepoint.powershell
In that case we see that the option is -name and the input is microsoft.sharepoint.powershell. You probably also noticed we left out the -name up above. That's because many commandlets have a default parameter. If there's no parameter specified PowerShell assumes that whatever comes next is the default parameter. This is very useful, but can also be a little dangerous if you're not careful, so when in doubt, just include the parameter.
One thing to note. If you're not familiar with autocomplete then OH MY GOODNESS YOU ARE MISSING OUT! Seriously, autocomplete is a revelation. My background is in Linux, so when I came to the Microsoft world and found out tab complete worked in PowerShell I was happy as a clam.
Anyway, as I was saying, in PowerShell you can hit tab if you're halfway through writing something and PowerShell will try to fill in the rest. So if you type "Get-comm" and hit tab it will finish out the "command." Or if you're typing out a super long file name you can get a few letters in and just hit tab and PowerShell will fill in the file name. It's amazing. So if you're not sure what options are available, you can also just put in a dash after a command and start going to town on the command key.