8/24/2012

SharePoint PowerShell cmdlets Equivalents for SharePoint 2007

 

SharePoint 2007 administrators may feel a bit left out when the see all of the articles showing how to do quick and powerful things with SharePoint 2010 PowerShell cmdlets. In this article I will show how to create common SharePoint objects without using the cmdlets.

This article is not done… I will be adding to it. Let me know if you need any other cmdlet replacements. When I get a chance I'll wrap up and publish my collection of 2007 "cmdlets" so you can generally just use the 2010 scripts.

 

Most of the examples below are in the form of "replace this cmdlet based code fragment with this SharePoint API fragment".

 

Hooking PowerShell to the SharePoint API

For all of the following script samples start your script with this line:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

This line is only need once at the beginning of your script.

 

Get-SPFarm

Replace:
  $farm = Get-SPFarm

With:
   $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local

Notes:

  • The cmdlet's default property is the name of configuration database. OM's default return is a list of all properties.
  • The output of the above can be piped just like Get-SPFarm.
      [Microsoft.SharePoint.Administration.SPFarm]::Local | select servers
    or
      $farm | select servers

 

Get-SPSite

Returned Data with URL parameter

Replace:
  $site = Get-SPSite http://intranet

With:
  $site = New-Object Microsoft.SharePoint.SPSite("http://intranet")

Notes:

  • The cmdlet's default property is the Url of the site collection. OM's default return is a list of all properties.
  • The output of the New-Object can be piped to another cmdlet just like Get-SPSite. You cannot pipe into New-Object.

 

Target of a Pipe passing a SPWebApplication or a collection of SPWebApplications

Replace:
$webapplication | Get-SPSite

With:
$webapplication | select -ExpandProperty Sites

Examples:
  2010: $webapplication | Get-SPSite | Select url, {$_.rootweb.title}
  2007: $webapplication | select -ExpandProperty Sites | Select url, {$_.rootweb.title}

 

Get-SPWeb

Returned Data with URL parameter

Replace:
  $site = Get-SPWeb http://intranet/sailboat

With:
  $web = (New-Object Microsoft.SharePoint.SPSite("http://intranet/blog")).OpenWeb()
or
  $web = (New-Object Microsoft.SharePoint.SPSite("http://intranet")).allwebs["sailboat"]

Notes:

  • The above is identical to the Get-SPSite replacement with the addition of .OpenWeb().
  • Get-SPWeb typically is used with "-Limit All" to display more than 20 webs. That is not needed with the 2007 equivalent which will always return all webs.

 

Target of a Pipe passing a collection of SPSites

Replace:
  collectionOfSites | Get-SPWeb

With:
  collectionOfSites | select -ExpandProperty AllWebs

Examples:
  2010: $webapplication.Sites | Get-SPWeb | Select title, url
  2007: $webapplication.Sites | select -ExpandProperty AllWebs | Select title,url

 

Get-SPWebApplication

Returned Data using URL parameter

Replace:
  $webApplication = Get-SPWebApplication http://servername

With:
$webApplication = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://servername")

 

Returned Data without parameter (return all web apps)

Replace:
$webApplications = Get-SPWebApplications

With:
$webApplications = [Microsoft.SharePoint.Administration.SPWebService]::ContentService.WebApplications

 

more to come…

5 comments:

Anonymous said...

Hey Mike,
Nice post - I've done a bunch with PS on 2010, and am now trying to utilize it to grab some metrics from a 2007 farm my company acquired in a merger.

Your post is a nice starting place for that!

- Jack
sharepointjack.com
(PS, I actually have a printed copy of your book on my shelf here at work!)

Unknown said...

How would I recreate the
Set-spsite cmdlet

Mike Smith said...

Christopher,

Creating an equivalent to Set-SPSite is doable, but as it has a number of options (and I have limited hours in the day :-) ) is there any subset that you are interested in? -QuotaTemplate, -MaxSize, etc.?

Mike

Unknown said...

Here's what I'm trying to replace
Set-SPuser -identity $ADGroup -web $sightcollection -group $groupname

I am attempting to add and AD group to a sharepoint permission group

Thanks for the quick response!

Mike Smith said...

Christopher,

You could use this for a one time addition:

$siteUrl = "http://sharepoint/sites/training"
$group = "Test Group"
$user = "SHAREPOINT\sales"

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.RootWeb
$web.EnsureUser($user)
$web.groups[$group].AddUser($web.AllUsers[$user])


Or this as a reuseable function:

function AddToGroup ($siteUrl, $group, $user)
{
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.RootWeb
$web.EnsureUser($user)
$web.groups[$group].AddUser($web.AllUsers[$user])
}


And then call the function like this:
addtogroup http://sharepoint/sites/training "Test Group" sharepoint\sales

Mike

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.