Nick Grattan’s SharePoint Blog

About Microsoft SharePoint and some .NET

Preparing PowerShell for SharePoint and MOSS 2007

with 15 comments

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.

 

 

 

 

 

About these ads

Written by Nick Grattan

September 3, 2007 at 11:19 am

15 Responses

Subscribe to comments with RSS.

  1. 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

  2. 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

  3. 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

    • 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

  4. I can install these on my workstation or on my dev sharepoint server?

    Stefanie

    January 13, 2011 at 3:14 pm

  5. [...] 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: http://nickgrattan.wordpress.com/2007/09/03/preparing-powershell-for-sharepoint-and-moss-2007/: [...]

  6. 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

  7. 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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 49 other followers

%d bloggers like this: