Getting started with PowerShell and SharePoint


Table of Contents

Step 1: Install PowerShell

PowerShell is standard on Windows Server 2008, Windows Vista and Windows 7. Any other Windows Operating Systems will require a 32-bit or 64-bit download of it from Microsoft web site.

Step 2: Execution Policy

To do a lot of things in SharePoint you will need to modify the Execution Policy. This is talked about more here and recommendations for UAT/Production environments are given.
In a development environment I would recommend running PowerShell and entering

Set-ExecutionPolicy Unrestricted
This is probably not appropriate in UAT/Production environments so please speak to your admins

Step 3: Create your PowerShell Profile

When PowerShell is installed you can create a profile so that everytime you run PowerShell common things are created.
To create your profile run

New-Item -path $profile -type file -force

It will be located at C:\Documents and Settings{username}\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1. More information on Profiles is available here

Step 4: Modify your profile

Open up your profile file and add these common functions and parameters

############################################################################
# Assumptions:
# - Running on machine with WSS/MOSS
# - C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN in path
# - For development servers only, does not dispose of objects
############################################################################
# Example usage:
# $list = get-splist -webUrl "http://site" -listName "Announcements"
############################################################################

$12HiveDir = "${env:CommonProgramFiles}\Microsoft Shared\web server extensions\12\"
cd $12HiveDir
[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint") | out-null
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server.Search") | out-null
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server") | out-null

# Returns the SPSite at the specified URL
function get-spsite ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
    return New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl"
}

# Returns the SPSite object from the specified URL
function get-spweb ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'))
{
    $site = New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl"
    return $site.OpenWeb()
}

# Returns the SPList object from the specified URL and list name
function get-splist ([String]$webUrl=$(throw 'Parameter -webUrl is missing!'),
[String]$listName=$(throw 'Parameter -listName is missing!'))
{
    $site = New-Object -TypeName "Microsoft.SharePoint.SPSite" -ArgumentList "$webUrl"
    $web = $site.OpenWeb()
    return $web.Lists[$listName]
}

Step 5: Inspect your Site Collection

You can now run up PowerShell and calls these functions

$site = get-spsite "http://yourserver"
$site
$site.Features

This should output some information on your Site Collection

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.


  1. Jun 26, 2009

    Jonathan Roussel says:

    Aren't these functions a bit dangerous as they don't allow for easy disposing of...

    Aren't these functions a bit dangerous as they don't allow for easy disposing of the SPWeb and Site objects ?

    I am not what one would called an experienced SP or PowerShell Dev, so I prefered to add a comment instead of modifying the page with what I think might be nice to add :

    Unknown macro: {NewCode}

    $site = get-spsite "http://yourserver"
    .... Do your Stuff
    $site.Dispose

    Unknown macro: {NewCode}

    $web = get-spweb "http://yourserver"
    .... Do your Stuff with the web
    .... As we lost reference to the SPSite object, I think (not sure at all) that we might get it back by using the SPWeb.Site property
    $site = $web.site
    $web.Dispose()
    $site.Dispose()

    Unknown macro: {NewCode}

    $list = get-splist "http://yourserver" "MyListName"
    .... Do your Stuff with the list
    $web = $list.ParentWeb
    $site = $web.site
    $web.Dispose()
    $site.Dispose()

    1. Jun 26, 2009

      Keith Dahlby says:

      You are correct that the objects should be disposed, with two caveats: SharePo...

      You are correct that the objects should be disposed, with two caveats:

      1. SharePoint will automagically dispose an unclosed SPSite when PowerShell is closed.
      2. If you're using PowerShell interactively, each new pipeline (every time code executes) will potentially run on a new thread, resulting in memory leaksthat won't be freed until the PowerShell session is closed---even if you do call Dispose().

      In practice, it's probably safe to let the objects leak as long as you don't leave PowerShell open on the server. These memory leaks only really matter if the code is called thousands of times, which most PoSh code isn't. On the other hand, if you have a long-running script that will open lots of SPSites/Webs, you will probably want to dispose properly.

      Other dispose-related links:

  2. Jun 26, 2009

    Anonymous says:

    To install PowerShell on Windows 2008, you can use the command "ServerManagerCmd...

    To install PowerShell on Windows 2008, you can use the command "ServerManagerCmd -install PowerShell".
    I have some examples of using PowerShell with SharePoint at codeplex.com/psbb (PowerShell Building Blocks for SharePoint).
    -Michael Blumenthal, http://blumenthalit.net (My SharePoint Blog) and @michaelbl on Twitter.

  3. Oct 16, 2009

    Ricky Elias says:

    Niklas Goude has written many helpful PowerShell scripts for SharePoint (Link).

    Niklas Goude has written many helpful PowerShell scripts for SharePoint (Link).


Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare