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: 0×80070005 (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