As with any new release, there was a lot to learn when SharePoint 2010 hit the streets. There were new features to master, and there were even some old features with which to get reacquainted. Typically, I’m up for such a challenge. Heck, I revel in it. However, one of the biggest challenges that I almost didn’t overcome was learning Windows PowerShell.
I’ve been working with SharePoint for a long time now. Although I started with SharePoint Team Services 2001, I really cut my teeth with Windows SharePoint Services 2.0 (WSS 2.0). I used it to manage a farm that had more than 1,200 site collections and more than 10,000 subsites, which are often referred to as webs. I learned very early on to love and embrace scripting.
I became especially good friends with the Stsadm command-line tool. Stsadm allowed me to manage the large farm and still have plenty of time to watch hysterical cat videos on YouTube. I was able to capitalize on my friendship with Stsadm. I wrote a TechNet Magazine article on Stsadm, a book chapter on Stsadm, and spoke at TechEd on, you guessed it, Stsadm.
Stsadm has been very good to me. Imagine my shock and horror when I found out that SharePoint 2010 was going to transition to PowerShell and my beloved Stsadm was being deprecated.
Until that moment in 2009, my exposure to PowerShell had been very limited. I had mostly avoided it because it seemed to “developery” for my tastes. It had objects, whatever those are. I’m a simple man. Stsadm, while limited and quirky, was easy to tame. PowerShell was complicated and bombastic. I honestly wasn’t sure if I was smart enough to trick it into doing my bidding or leverage it like I had Stsadm. I found myself in the first of the seven stages of grief.
After I made it to the seventh stage, Acceptance, I got back up on my horse and starting trying to conquer this beast. I bought a couple of PowerShell books and looked for PowerShell support groups in my area. Nothing seemed to work. I still wasn’t “getting” PowerShell. I still couldn’t do anything with it besides adding two numbers together and writing “Hello World” on my screen. Neither accomplishment would look too impressive on a resume. Maybe I wasn’t smart enough to use PowerShell. Maybe the Wendy’s restaurant down the street could use a new fry cook or Frosty maker.
Lesson 1: Just Use It
One day it finally hit me, I was going at this backward. Instead of learning the language so that someday I could use PowerShell to get tasks done in SharePoint, I should start trying to automate daily SharePoint tasks as a way to help me learn the language. Of all the PowerShell lessons I’ve learned, this one is the most important. That was the day the skies opened up and the sun shone down on me and PowerShell.
Lesson 2: Make Get-Member Your Hero
My first scripts were pretty mundane and basic. It was simple stuff like getting a list of site collections with Get-SPSite or a list of webs with Get-SPWeb. Although they were basic, they did ease me into concepts like PowerShell’s pipeline and how to format command output with cmdlets such as Select-Object and Format-Table. I was able to write handy one-liners like this:
Get-SPSite -Limit all |
Select-Object Url, Owner, SecondaryContact |
Format-Table -AutoSize
(Although this command wraps here, you'd enter it all on one line in the PowerShell console. The same holds true for the other commands that wrap.) This one-liner returns a handy list of all the site collections in the farm, along with each site collection’s owner and secondary owner.
In this one-liner, it’s intuitive that the Owner property stores the name of the owner, but the same can’t be said for SecondaryContact property, which stores the name of the secondary owner. An object’s properties and methods aren’t always intuitive, which is one of PowerShell’s little quirks that contribute to its bad reputation and high learning curve.
Fortunately, PowerShell has the Get-Member cmdlet, which is my hero because it has come to my rescue on many occasions. You can use it with any object to learn about that object’s properties and methods. For example, you can see all the properties of the SPSite object with the following command:
Get-SPSite | Get-Member