PowerShell for SharePoint Admins–The REAL Primer–Series #5

This is the fifth entry of my PowerShell for SharePoint Admins series. In this session we will learn some PowerShell cmdlets that will give us some flexibility in being able to accomplish the Top Ten things for which SharePoint Admins use PowerShell . I am basing this series on what SharePoint Administrators have said are the most important tasks to accomplish in PowerShell. 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

In this session, as all the previous sessions, we are continuing on in our quest to become proficient in this shell language. We need to understand some of the “funny” symbols and stuff (remember, we’re OSITA’s and don’t want nuthin’ to do with “Codin’”) ---sigh…it’ll be OK, I promise.


Most of the symbols used in PowerShell you’ve seen before and aren’t that new. Some will be new, but I will attempt to make it as painless as possible.

! = NOT. As in, if you see this symbol before something, it means NO/NOT/Does not exist. Where would you see this? Examine the following line:

  (!(Test-Path $profile.AllUsersAllHosts)) 
 This would read (in English) = If the “All Users All Hosts” profile directory does NOT exist….that’s all that means.
  { or }  = block of stuff.  More or less.  When you want PowerShell to treat something as a whole. 
 Here is an example:  
 C:\> Get-SPWeb http://portal/* | where {$_.MasterUrl -like “*/v4.master”} 

OOH…I slipped two on you that time! I will explain both the {} (called curly braces) and the | (called a pipe).

In English the line above says, get all the sites (not site collections) that start with the URL http://portal/* (That is the Get-SPWeb) and then when that is done (that is where the pipe (|) comes in), show wherever there are those that are using the v4.master page to me.


$ = variable. This is self-explanatory. Here is an example:

    1:  $SiteName = "Slick Rick's Site" 
    2:  $SiteUrl = "http://intranet.osita.com/sites/" + $SiteName   
    3:  $NewSite = New-SPSite –URL   
    4:  $SiteUrl -OwnerAlias Administrator -Template STS#0 –Name $SiteName  
    5:  $RootWeb = $NewSite.RootWeb
    6:  $RootWeb.Title = "Slick Rick's Site: " + $SiteName
    7:  $RootWeb.Update()
    8:  Write-Host "Site successfully created"
 So what we have above is this: We are creating a variable named $SiteName and giving it the name “Slick Rick’s Site”.  So everywhere you see $SiteName, PoSh will stick in “Slick Rick’s Site”.  Follow that logic for each of the lines that begin with ‘$’.  Line 3 is a little different in that it is saying that the cmdlet “New-SPSite” will be substituted for $NewSite.  So you can see that even cmdlets/commands/actions can be used in variables.

Pulling it all together

 OK, so now is your test.  Jeapordy-style.  I will give you the answer and then you will give me the question: 
 “This script means:  If there is no entry for $FarmPassphrase or if the entry for $FarmPassphrase is blank, tell the user to enter one.
 start-Music.Jeopardy<br>{<br>start-sleep 30<br>} <br>stop-Music.Jeopardy
 OK! The Question is:
 What does the following line of script mean? 
 If (!($FarmPassphrase) -or ($FarmPassphrase -eq ""))      {<br>        Throw " - You must enter a Farm Passphrase"<br>    } 

Did you get it?  If so, YAY!  If not, re-read this post until you understand the concepts.  In the next session, we will configure Service Applications with corresponding databases.