in

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.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

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.

$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
}

PowerShellSiteCollectionFeatureActivate

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
}

PowerShellSiteCollectionFeatureDeactivate

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.

Comments

 

SharePoint Daily » Blog Archive » SharePoint Information Architecture; ARM Will Run Office 15; Microsoft Ruins Valentine’s Day said:

Pingback from  SharePoint Daily  » Blog Archive   » SharePoint Information Architecture; ARM Will Run Office 15; Microsoft Ruins Valentine’s Day

February 10, 2012 7:54 AM
 

SharePoint Daily said:

Cloud computing could learn a few things from Madonna . - Dooley Top News Stories SharePoint Information

February 10, 2012 7:59 AM
 

SB Chatterjee said:

good post.. posted this link on Twitter..

February 10, 2012 9:38 AM
 

Julius said:

Your post helped me a lot!

February 17, 2012 2:55 PM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an Applications Architect at Infusion specializing in ECM and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems