How to: Activate a feature on multiple site collections using PowerShell
Posted
Thursday, February 9, 2012 8:40 PM
by
CoreyRoth
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.
$feature = Get-SPFeature PublishingSite
$siteCollections = Get-SPSite –WebApplication http://sp2010
$siteCollections | foreach-object {
Write-Host "Activating" $feature.DisplayName "on" $_.Url
Enable-SPFeature $feature -Url $_.Url
}
Of course this works with deactivation as well too. I just updated the script to use Disable-SPFeature instead.
$feature = Get-SPFeature PublishingSite
$siteCollections = Get-SPSite –WebApplication http://sp2010
$siteCollections | foreach-object {
Write-Host "Deactivating" $feature.DisplayName "on" $_.Url
Disable-SPFeature $feature -Url $_.Url -confirm:$false
}
Note that I added –confrim:$false to the Disable-SPFeature line to prevent being prompted by the script.
This is a simple script that makes it easy to activate and deactivate features in bulk. This same logic could be applied to site scoped features as well if you had the need.