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