Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

How to: Activate a feature on multiple site collections using PowerShell

A lot of times we have a need to activate a feature on multiple site collections. This could be a custom feature to do branding or you may simply be activating publishing on multiple site collections. PowerShell makes tasks like these easy. We’ll take what we learned from my original Activating Features with PowerShell post and use some common techniques to iterate through the site collections and activate the desired feature.

For this example, we’ll activate the SharePoint Server Publishing Infrastructure. The internal name of that feature is actually PublishingSite so we’ll start by using PowerShell to get a reference to that feature. We’ll assign the result of that value to a variable.

$feature = Get-SPFeature PublishingSite

If this is your first time working with this feature, be sure and test it by typing it into PowerShell to ensure that you are getting the reference to the feature correctly. Once you have the feature we can get a list of site collections. Retrieving site collections is simple.

$siteCollections = Get-SPSite

However, this will return every site collection from every web application. You probably want to limit it to a single web application using the –WebApplication parameter like this:

$siteCollections = Get-SPSite –WebApplication http://sp2010

We can then just use ForEach-Object to iterate through each site collection and then activate the feature. I use a pipe bind with the $siteCollections variable. Then I just use the Enable-SPFeature command passing it $feature for the feature and the Url to the site collection which we can get from the Url property of the $_ object. I also wrote some output to the screen so we could see which site collection was activating. Here is what the whole script looks like.