Preparing PowerShell for SharePoint and MOSS 2007
Microsoft PowerShell allows you to write scripts that can use .NET classes, objects, properties and methods. It therefore provides an ideal way of creating script files by calling into the SharePoint and MOSS object model.
First, download Microsoft PowerShell from here and install. PowerShell can then be lanched from the “Start” menu.
The PowerShell execution policy determines if script files can be run – by default they are prohibited for security reasons. Enter the following command to find out about execution policy and signing script files:
get-help about_signing
You can either elect to only run signed scripts (in which case scripts must be signed with a certificate), or to run unsigned scripts from the local file system or to run any unsigned script. For most users, allowing unsigned scripts to run from the local file system is acceptable, so to allow this enter the following commands to set and then retrieve the execution policy (you need to be logged on as administrator):
set-executionpolicy RemoteSigned
get-executionpolicy
Before programming against the SharePoint and/or MOSS 2007 object model the appropriate assemblies must be loaded. The following commands do this:
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)
You can place these commands the file called Microsoft.PowerShell_profile.ps1 in a folder called My Documents\WindowsPowerShell to run commands automatically when you run PowerShell (the folder and the file may not exist and may need to be created).
You can now test calling into the SharePoint object model. The following command gets a reference to a site collection at the given URL:
$spsite=[Microsoft.SharePoint.SPSite](“http://localhost/sites/intranet“)
You can review all the property values for the SPSite object by typing the name of the object variable:
$spsite
You can find an example of creating a script file for chaning a site’s regional settings here.
Excelent article, one small comment – you can use LoadWithPartialName method to load SharePoint assembly:
[system.reflection.assembly]::loadwithpartialname(“Microsoft.SharePoint”)
Mile Grujovski
April 29, 2008 at 8:51 am
Mile, Thanks for your comment and tip. I’ve seen a number of articles that suggest LoadWithPartialName may be dangerous if there are multiple versions of an assembly. However, this is likely to be an issue with SharePoint so your suggestion makes life much easier! Nick.
nickgrattan
April 29, 2008 at 9:02 am
I keep getting this error when typing the $spsite variable:
format-default : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
I am able to load the assembly and instantiate the object. The user under which I am running powershell has access to the Sharepoint site to which I am trying to connect. What permissions do I need to change to get access to the SPSite instance?
Thanks!
Tzuriel
June 6, 2008 at 4:00 pm
I am guessing you should start PowerShell as an administrator and you need to have the sysadmin permission in the SQL Database. I am not sure if these are the permissions you need but give it a try.
Try those, might work as even I used to get the same access denied.
Regards
Vishwas
Vishwas S
June 21, 2010 at 7:58 pm
Hi,
I am getting the same kind of access denied error.
Wondering why should I have a exclusive rights to the Content Database when having rights to the SiteCollection/Site should be enough?
It doesnt make sense for a normal powershell user to request SQL engineers to provide permissions to the content database which might be hosting other siteCollections/Sites which i shouldnt be reading
Please advise.
Thanks,
Karthik
Karthik
March 16, 2011 at 4:21 pm
I can install these on my workstation or on my dev sharepoint server?
Stefanie
January 13, 2011 at 3:14 pm
You will need to install them on the dev server. regards, Nick.
Nick Grattan
January 13, 2011 at 9:53 pm
[…] Step 1: Launch PowerShell and load SharePoint assemblies to the program. Refer to this blog post by Nick Grattan in order to prepare your PowerShell for SharePoint 2007: https://nickgrattan.wordpress.com/2007/09/03/preparing-powershell-for-sharepoint-and-moss-2007/: […]
SharePoint 2010 Cookbook: 4 Methods to Migrate a Single List to SharePoint 2010 from 2007 - SharePoint 2010 - Bamboo Nation
February 10, 2011 at 2:26 am
Hi !!
I work with MOSS 2007 and would like to automate a few things using Powershell. Your post helped me do .NET coding using Sharepoint object model. but now I need to use the inbuilt SharePoint cmdlets. However it is not available in my Powershell. Any ideas why??
Suheil
May 18, 2011 at 4:16 am
HI Suheil, Do you mean StsAdm command? You can call these directly from PowerShell by calling stsadm from your scripts. Ensure that you either fully qualify stsadm with a folder path. Regards, Nick.
Nick Grattan
May 18, 2011 at 6:49 am
I did not mean stsadm command. What I meant was as there are built-in cmdlets in Sharepoint 2010 like Get-SPSite, Get- SPWeb etc, do we have such cmdlets for SharePoint 2007. If yes, then how do we enable them?
Suheil
May 23, 2011 at 2:53 am
Suheil, To do this you will need to:
SharePoint 2007:
1. Load the SharePoint object model into PowerShell:
Add-Type -AssemblyName “Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
2. Reference object model methods, properties etc. E.G. to get a list use
$spSite = new-object Microsoft.Sharepoint.SPSite($spSiteUrl)
$spWeb = $spSite.OpenWeb()
$spDocLib = $spWeb.Lists[$docLibName]
where $spSiteUrl contains the site’s URL and $docLibName the name of the library or list.
SharePoint 2010
Run PowerShell from the “Microsoft SharePoint 2010 Products” menu and use cmdlets like Add-SPSolution. You can also use “Add-PSSnapin Microsoft.Sharepoint.Powershell” to load the cmdlets into other scripts.
HTH, Regards, Nick
Note: Reply updated 26-May-2011
Nick Grattan
May 23, 2011 at 12:55 pm
HI Nick,
Please, could you xplain deeper hot to do for use SP 2010 cmdlets in SP2007? I’m trying for 2 weeks and I still can’t do it, 😦 please help me dude!
Daniel
Daniel
October 25, 2011 at 6:34 pm
Hi Daniel, To the best of my knowledge this is not possible. The cmdlets use the SharePoint 2010 object model classes and these are incompatible with 2007. Regards, Nick.
Nick Grattan
October 25, 2011 at 6:40 pm
[…] For creating pages in pages library:Install powershell:http://www.microsoft.com/en-us/download/details.aspx?id=10512reference:https://nickgrattan.wordpress.com/2007/09/03/preparing-powershell-for-sharepoint-and-moss-2007/ […]
Publishinh Feature gets crashed in MOSS 2007 | Jyothsna Radha
July 18, 2013 at 11:57 am
Thanks! PowerShell cmdlets, like Get-SPWeb, Get-SPSite, etc are designed for SharePoint 2010. But We can Write our own Custom PowerShell functions to get the similar functionality.
Read more: How to use PowerShell with MOSS 2007
Adam
August 18, 2013 at 3:03 pm
How can I get the list of failed and aborted timer JOB STATUS in Sharepoint Server 2007 using powershell. Can anyone please help??
Praveen Kumar
August 21, 2014 at 4:47 am
One of the assemblies are not loading for some reason “[System.Reflection.Assembly]::Load(“Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”) ”
Giving an error, couldn’t load the assembly . Let me know
kevin
January 5, 2017 at 5:57 pm