Enable-Activate a SharePoint Feature on All Site Collections using PowerShell

Ever wanted to enable a feature on all site collections in a SharePoint environment? Well, To activate a site collection feature in SharePoint, navigate to Site settings >> Site Collection Features >> Click on "Activate" next to the relevant feature.
How to activate a feature in SharePoint using PowerShell
How to Activate a Feature using PowerShell?
To activate a SharePoint feature on a particular site, we use: Enable-SPFeature cmdlet.
Enable-SPFeature –Identity "FeatureName" –url http://SharePoint-site-URL  
Recently I had to enable a Branding feature on all sites to apply new corporate branding with the following one liner: PowerShell script.
#activate feature on all sites in all web applications in the farm
Get-SPWebApplication | Get-SPSite -Limit ALL | ForEach-Object{ Enable-SPFeature -URL $_.url -Identity (Feature Name or URL) -Confirm:$FALSE } 

Here is the E.g. for above script:
Get-SPWebApplication "http://www.sharepoint.crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit ALL | ForEach-Object { Enable-SPFeature  "fead7313-ae6d-45dd-8260-13b563cb4c71" } -Url $_.Url
Here the PowerShell script gets all sites under the web application and activates the feature.

To get the Feature GUID you can use:
Get-SPFeature | Where {$_.DisplayName -eq "FeatureName"} | Select ID
In total, The below script iterates through all web applications, site collections, checks whether the feature is already activated. If not, enables feature on all sites .
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get all Site collections
$SiteColl = Get-SPWebApplication "http://sharepoint.crescent.com" | Get-SPSite -Limit All 
#Target Feature to Activate
$FeatureToActivate = Get-SPFeature | Where {$_.DisplayName -eq "FeatureName"} 

#Iterate through each site collection
ForEach($Site in $SiteColl)
{
   #Check if Feature is already activated
   $FeatureActivated = Get-SPFeature -site $site | Where {$_.displayname -eq $FeatureToActivate.DisplayName}
 
   if($FeatureActivated -ne $null)
   {
      Write-Host "Feature already activated at: "$site.Url
   }
   else
   {
      #Activate the feature
      Enable-SPFeature -Identity $FeatureToActivate -URL $Site.URL -Confirm:$False
      Write-Host "Activated Feature on "$site.Url
   }
} 
Enable-Activate a SharePoint Feature on All Site Collections using PowerShell Enable-Activate a SharePoint Feature on All Site Collections using PowerShell Reviewed by Salaudeen Rajack on August 05, 2013 Rating: 5

1 comment:

  1. We have a feature that is installed on three web applications on a single SharePoint farm however we can have only one of the 3 activated at any time and we need to periodically change which is active for testing purposes. Because this happens randomly and potentially more than once a day we don't want to have to keep pestering our SP admin so we would like to script the changes and set it up so others can run the script. As a result I was thinking we could set up a windows scheduler task/s which run/s a PowerShell script and then we enable PSRemote and potentially run the task from multiple places using PowerShell as well. If you could confirm this is doable and if possible an initial go at the server side feature activation/deactivation script it would be appreciated. I was thinking the script would initially deactivate them all and confirm this and then activate the selected one by some sort of parameter that is passed in ie. the web application path etc. Thank you in advance for your help. Mat

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.