Wednesday, 9 March 2011

Using PowerShell scripts with WSS 3.0 and SharePoint 2007

Although Windows SharePoint Services 3.0 and SharePoint Server 2007 do not include built-in PowerShell cmdlets, this does not stop you using PowerShell to manage these servers and content where the supplied STSADM commands fall short in functionality or flexibility.

You can install Windows PowerShell 2.0 (which includes the ISE Windows application) from any Windows 2003 or 2008 server by downloading it from Windows Update. Once you have the console started, simply load the SharePoint assemblies with the following command before you start scripting:


If you want to load this command every time you launch the PowerShell console or ISE application, add it to a file called profile.ps1 and copy this file into a folder called WindowsPowerShell in the My Documents folder for that user. For an ISE-specific profile, copy the command into a file called Microsoft.PowerShellISE_profile.ps1 in the same folder.

You will also need to set the execution policy on the server to RemoteSigned if you want to be able to run PS1 scripts locally. This can be set with the following command:

Set-ExecutionPolicy RemoteSigned

Once set, the configuration will remain fixed on the server unless it is reset by an administrator or through Group Policy. Type get-help about_Execution_Policies at a PowerShell command line for more information on this.

I find the majority of scripts used to manage a SharePoint 2010 environment involve use of the Get-SPSite and Get-SPWeb cmdlets to call a site collection or site respectively. Although we do not have these cmdlets available to us in SharePoint 2007, there are alternative ways of retrieving these objects.

To assign a site collection object to a variable, use the following command, replacing http://portal with the URL of your site collection:

$site = New-Object Microsoft.SharePoint.SPSite(“http://portal”)

To open the root site of the site collection and assign it to a variable, type the following:

$web = $site.RootWeb

If you need access to a specific site in a site collection, you can specify the relative path to the site with the OpenWeb method. For example, to get a site at the URL http://portal/subsite1/subsite2, type the following command:

$web = $site.OpenWeb(“subsite1/subsite2”)

Once you have assigned these objects to variables, you can use them in the same way as with SharePoint 2010. For example, to change the title of a site, type the following commands:

$web.Title = "New Title"

Also bear in mind that unless the commands you are using have been deprecated in the object model or you are trying to access functionality that no longer exists, most PowerShell scripts written for SharePoint 2007 will work with SharePoint 2010. Therefore, if you are managing both environments, you may find it a more supportable option to write your scripts for SharePoint 2007 first and then test for 2010 compatibility.

The scripts I publish on this site are generally compatible with SharePoint 2010 only, so to help you translate these scripts into SharePoint 2007 versions, here are a few useful SharePoint 2010 cmdlet alternatives:

Description SharePoint 2010 SharePoint 2007
Assign a Web Application object to the $webApp variable $webApp = Get-SPWebApplication http://portal $webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup(“http://portal”)
Assign the AD account “DOMAIN\SP_Admin” to the $user variable $user = Get-SPUser -Web $web -User "DOMAIN\SP_Admin" $user = $web.EnsureUser("DOMAIN\SP_Admin")
Assign the local SharePoint Farm object to the $farm variable $farm = Get-SPFarm $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
Get a named feature from the SharePoint Farm and assign it to the $feature variable

$feature = Get-SPFeature "FeatureName"

$feature = $farm.FeatureDefinitions["FeatureName"]
Activate a site-scoped feature on a site collection

Enable-SPFeature $feature -Url $site.Url

Deactivate a site-scoped feature on a site collection Disable-SPFeature $feature -Url $site.Url -Force $site.Features.Remove($feature.ID)
Use this command when making a call to a service application (2010) or shared service (2007). For example, it is used when gaining access to the User Profile Manager object model for managing user profiles in a script

$context = Get-SPServiceContext $site

$context = [Microsoft.Office.Server.ServerContext]::GetContext($site)

I will update this table above over time to include any other SharePoint 2007 alternatives to SharePoint 2010 cmdlets as I come across them.


  1. Can these scripts be run from a non-Sharepoint server machine if the DLL/s are available?

  2. Wonderful post however I was wondering if you could write a little more on this topic? I’d be very thankful if you could elaborate a little bit further. Thank you!
    SharePoint 2013 Development training Online

  3. how would I replace the Set-SPUser? as in this:
    Set-SPUser -Identity $ADGroupSPFriendly -Web $SiteCollection -Group $SPGroupName


  4. الاول سيو افضل شركة تسويق الكترونى تعمل على تسويق المواقع وارشفتها باحدث وسائل السيو فهى تقوم بتهيئة المواقع لمحركات البحث لجلب الزوار والعملاء
    ومن اقسام الاول سيو قسم الخدمات المنزليه مثل افضل شركة تنظيف بالرياض
    وافضل شركة تنظيف شرق الرياض بالاضافة الى افضل شركة مكافحة الحشرات بالرياض وتغطى خدماتها شرق وشمال الرياض فالاول افضل شركة مكافحة حشرات شرق الرياض تستخدم اقوى المبيدات الحشرية
    واسعارها في متناول الجميع ومن الهام التاكد دائما من خلو منزلك من تسربات المياه حتى لا تتعرض لمشاكل مثل انهيار وتصدع المبانى الاول افضل شركة كشف تسربات المياه بالرياض تستخدم اجهزه الكترونية اكشف التسرب بالذبذبات وتغطى كافة مناطق الرياض فهى افضل شركة كشف تسربات المياه شرق الرياض
    شركة نقل عفش بجدة
    شركة تنظيف خزانات بالقطيف

    شركة تنظيف كنب بالرياض

    الاول شركة تخزين اثاث بالرياض
    توفر افضل المستودعات لتخزين الاثاث والعفش التى تتوفر بها كل عوامل الامن للحفاظ على الاثاث وتقوم بتغليف الاثاث قبل تخزينه للمحافظة عليها فالاول افضل شركات تخزين العفش بالرياض ونوفر سيارات لنقل الاثاث من اى مكان داخل المملكة .
    شركة تنظيف خزانات بينبع

    بالاضافة الى خدمة نقل اثاث بالرياض فنحن نمتلك اكبر اسطول نقل بالرياض يوفر لك افضل خدمة نقل اثاث بالرياض
    بالاضافة الى ان اسعارها في متناول الجميع فهى فالاول شركة نقل اثاث بالرياض رخصية بالمقارنة مع باقي الشركات بالرغم من جودة عملها ودقة المواعيد فهى تقوم بتغليف العفش للحفاظ عليه اثناء النقل وتقوم باستخدام سيارات مغطاه مخصصة لنقل العفش للحفاظ عليه من اضرار الشمس والامطار والاتربة فالاول افضل شركة نقل اثاث بالرياض وتغطى كافة مدن المملكة
    نقل اثاث بالرياض

    وان كنت بحاجة الى شركة شراء اثاثك القديم المستعمل فباتصالك بنا تحصل على افضل شركة شراء اثاث مستعمل بالرياض والخرج
    حقين شراء اثاث مستعمل بالرياض