How to use PowerShell with MOSS 2007?

PowerShell has become a 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 with 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, it’s natively supported. Just download and install PowerShell 2.0 from Windows 2003/2008 server from Microsoft site https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/install/installing-the-windows-powershell-2.0-engine?view=powershell-7.1. You can refer to my post: Install PowerShell in Windows 2003 for more step by step procedures.

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:

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

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

Let’s 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("https://sharepoint.company.com")
 
 #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:

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

#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)
            {
               $web=$site.OpenWeb();      
            }
    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))

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

4 thoughts on “How to use PowerShell with MOSS 2007?

  • hello. nice post. thank you.

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

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

    Reply
  • Thanks, did help! Although had to disable your Javascript so I could Evernote this useful snippet 🙁

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *