How to use PowerShell with MOSS 2007?

Powershell has become more widely used platform to administer and automate common administrative tasks. Can we use PowerShell with SharePoint 2007? Because I was told PowerShell cmdlets (like Get-SPWeb, Get-SPSite, etc) are designed for SharePoint 2010.  Can we use PowerShell for SharePoint 2007?

Of course, You can! As PowerShell allows us to use .NET assemblies, we can use SharePoint objects, properties and their methods as we use object model in C# or any other .Net language. You can use PowerShell to manage SharePoint 2007, but not the cmdlets for SharePoint 2010, as SharePoint 2010 powershell snap-in or cmdlets are incompatible with SharePoint 2007. Getting started powershell in SharePoint 2007:
  1. Install PowerShell on SharePoint Servers
  2. Set Executing Policy
  3. Load necessary SharePoint Assemblies
  4. Write PowerShell Code using SharePoint Object Model
1. Install PowerShell on moss 2007 Servers
To run PowerShell on MOSS 2007, we must Install it first. If you are running SharePoint 2007 on Windows 2003, You have to download and install PowerShell. In Windows 2008, its natively supported. Just download and install PowerShell 2.0 from Windows 2003/2008 server from Microsoft site can refer my post : Install PowerShell in Windows 2003 for more step by step procedure.

2. Set Executing Policy to RemoteSigned (just one time) 
After installing PowerShell, the next step would be setting ExecutionPolicy. By default the policy is set to "restricted". Check it by running (Get-ExecutionPolicy)

Set-ExecutionPolicy RemoteSigned
It allows  all scripts running from servers locally are allowed.

3. Load SharePoint 2007 Assemblies:
To use PowerShell in SharePoint 2007, Fire the PowerShell console from Start > All Programs > Accessories > Windows PowerShell, simply load the SharePoint assemblies with the following command before you start scripting:


we can't use Get-SpWeb or Get-SpSite cmd-lets, but we can create the objects from the SharePoint Assembly. Here is how:

4. Write PowerShell Code using SharePoint Object Model 
Lets write our PowerShell script in SharePoint 2007 to enumerate sites and list all sub-site titles,  URLs.
#SharePoint 2007  powershell examples

#Load SharePoint Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") >null
 #Get Site Collection
 $Site=new-Object Microsoft.SharePoint.SPSite("")
 #list all site collections
 foreach($SiteColl in $Site.WebApplication.Sites)
    #Loop through all Sub Sites
       foreach($Web in $SiteColl.AllWebs)
     #Output Site title and URL
  Write-Host $web.Title: $Web.Url

You can access the members of the $Site object by typing: $Site | Get-Member
use powershell sharepoint 2007
Its also possible to create your own functions/cmdlets, which does the similar thing as SharePoint 2010 Cmdlets. Powershell cmdlets for SharePoint 2007:

Get-SPWebApplication Cmdlet for SharePoint 2007:
#Get All Web Applications
Function global:Get-SPWebApplication($WebAppURL)
 if($WebAppURL -eq $null)  #Get All Web Applications
  #Sharepoint 2007 powershell spfarm
  $Farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
  $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
  $WebApps = @()
  foreach ($websvc in $websvcs) {
      foreach ($WebApp in $websvc.WebApplications) {
          $WebApps+ = $WebApp
  return $WebApps
 else #Get Web Application for given URL
  return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)

Get-SPSite Cmdlet for SharePoint 2007:

#Using Get-SPSite in MOSS 2007
function global:Get-SPSite($url)
    return new-Object Microsoft.SharePoint.SPSite($url)

#Get the Site Collection
$SiteColletion = Get-SPSite($SiteCollURL)

Get-SPWeb cmdlet for SharePoint 2007
Function global:Get-SPWeb($url)
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null)
    return $web
These powershell functions for SharePoint 2007 does the same thing as in SharePoint 2010. You can reuse them when writing powershell scripts for moss 2007.

SharePoint 2007 PowerShell Permissions:
Be sure you are running PowerShell on the SharePoint server as an administrator and your account has sysadmin permission in the SQL Database. Otherwise you may get: Access is denied. (Exception from HRESULT: 0×80070005 (E_ACCESSDENIED))
How to use PowerShell with MOSS 2007? How to use PowerShell with MOSS 2007? Reviewed by Unknown on May 18, 2011 Rating: 5


  1. Thanks, did help! Although had to disable your Javascript so I could Evernote this useful snippet :(

  2. It would be useful to have an example of how to write a script that demonstrates how to reference the get-spsite, etc.

  3. Great post. Saved me lots of time with the get-spweb.

  4. hello. nice post. thank you.


Please Login and comment to get your questions answered!

Powered by Blogger.