PowerShell for SharePoint Admins–The REAL Primer–Part 3

I gave a talk last weekend at SharePoint Saturday NYC and took a poll. I asked the audience what were the 10 most common things SharePoint Administrators did in their jobs. Here is the list:

  1. Create/Delete Sites, Webs and Web Applications

  2. Backup and Restore Sites, Webs and Web Applications

  3. Deploy Solutions and Features

  4. Add/Delete Users

  5. Logs

  6. Configure Service Applications

  7. Manage Content Databases

  8. Manage Timer Jobs

  9. Manage Content Deployment

Oh and before I forget, my slide deck is posted here on SlideShare.

OK, so just so you know—blogging faithfully is a challenge for me; I will simply start a series. I thought that Part 3 was going to be the final piece but I can’t see doing the Top 10 all in one post—I just cant do that and post frequently, SO—I will devote a post each week to each of the Top 10. 

We are going to create a Web Application to house a site collection that will have a site which will house an external list (yup we will provision BCS using PowerShell—fun, fun). We will back it up, delete it, then restore it (all using PowerShell), install and deploy a feature, add a user, move the site from one content database to another, and much much more.

So as an OSITA (Old School IT Admin), this can seem like a tall order; almost a reason NOT to want to use PowerShell because, well…I HATE coding or having to learn how to do ALL THAT at the command line. Well, don’t fret my fellow Admins—it’s not THAT BAD—actually not bad at all. I promise you, once you learn a few things it will become second nature to you, just like typing CLS to clear the screen or DIR to get the directory.

Create/Delete Sites, Webs and Web Applications

Yes, you can.  You can create a site via Central Admin and you ask “So why would I ever use PowerShell to create a site?”  I can think of two reasons: a) you can’t get to CA for some unknown reason or b) you try and it doesn’t work.  Let me give you a couple of scenarios:

You are a SharePoint admin and you need to create a site for a group that is impatient.  From the time you receive the request they are knocking on your door (like you don’t have a million other things to do) and so you say, fine—how long can this take?  You type in the URL for Central Admin and it churns….and churns…hmmm, you say..that’s strange.  You close the browser and try again.  Nada.  hmmm..you RDP into the server and try from there.  503 error.  You go to IIS and see what’s up and the phone rings “Is it done yet?” 

It probably took you 30 sec or less just to read the above paragraph, but it would have taken you much longer to actually do what the paragraph says.  In that same amount of time you could have simply typed a simple cmdlet and been done with it.

Here’s the second scenario:  You are a SharePoint admin and you need to create a site for a group that is impatient.  Same thing…except CA works, but when you try to create the site, it fails.  Multiple times.  Multiple templates.   Ok, so let’s say that for some reason the Timer Job Service is off and won’t turn back on.  What do you do?  You could spend time on working on that while the group keeps calling you or knocking on your door OR you could simply type a simple cmdlet and be done with it.

You see what I’m getting at?  PowerShell will save you TONS of time and it’s SIMPLE.  Especially creating sites.  Here is how you do it:

Go to the run command and type PowerShell.  OOOH, what a lovely blue background (for those who read and followed the previous post).

We will create a web application to host our site collection.  Let’s call our Web Application OSITA.com.  (If you are following this post by doing the exercises, make sure you put an entry in your hosts file so that this works).

Cool Tricks

So at the PowerShell prompt, type:  New-SPWeb and hit the TAB key until the entire cmdlet reads: New-SPWebApplication.  Then put a space after it, type a hyphen and hit the TAB key once more.  What you should now see is the following:

 New-SPWebApplication -Name

What this cool little trick does is help you put in the information that you need to make the script run without errors.  So, type in the name of the Web Application as “OSITA”, then type a space and a hyphen and hit the TAB key two times.  It should now ask for the Application Pool.  Let’s say OSITAAppPool.  So now the line should look like this:

 New-SPWebApplication -Name OSITA -ApplicationPool OSITAAppPool

I can tell you’re liking this TAB key thing, huh?  You just keep hitting the TAB key to cycle through the various parameters and typing in the values. The parameters you really want to use in this example are the following:

-Name “OSITA”

-ApplicationPool “OSITAAppPool”

-Port “8080”

-ApplicationPoolAccount “<your app Pool account>”

-URL “http://osita.com”

When all those (and only those) parameters are typed in hit ENTER and it will churn for a little bit.  When it is all done you should get a display similar to the following:

 DisplayName                    Url-----------                    ---OSITA                          http://osita.com:8080/


Now we are going to create a root site collection in this web application to use.  Let’s keep the name the same. Remember to keep everything in quotes.  Here is the list of parameters—I will let you type it all in using the TAB key trick I taught you above:

-Name “My First PoSH Site”

-URL “http://osita.com:8080”

-Template STS#0 (NOTE:  This does NOT go in quotes—this template is the Team Site Template, more on those later)

-OwnerAlias “<your_domain\your_username>”

-Description “Slick Rick taught me how to use PowerShell to create this site”

-Confirm:$false (Type that in exactly as you see it.   This will keep the prompt from asking you “Are you sure?”

OK, so for those of you who want a *picture* here it is:

 New-SPSite -Url "http://osita.com:8080" -Template STS#0 -Name "My First PoSH Site" -Description "Slick Rick taught me how to use PowerShell to create this site" -OwnerAlias "mcc\rtaylor" -Confirm:$false

Now hit enter.  It will churn and then you’ll get the lovely confirmation that your site is now created.  But did it *really* work?  Open up a browser and type in the URL (including the port number) and see.

It should prompt you for credentials and after you do that….voila!  How cool is *THAT*!?  You’ve done it!  I recommend doing another and another until you are confident you have it down to a science. 

Next week: We’ll back up our site, delete it and then restore it.