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:
- Install PowerShell on SharePoint Servers
- Set Executing Policy
- Load necessary SharePoint Assemblies
- 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
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:
hello. nice post. thank you.
Great post. Saved me lots of time with the get-spweb.
It would be useful to have an example of how to write a script that demonstrates how to reference the get-spsite, etc.
Thanks, did help! Although had to disable your Javascript so I could Evernote this useful snippet 🙁