Saturday, February 13, 2016

Activate Feature on All Sites in SharePoint using PowerShell

In some scenarios, you may have to activate a feature for all sites or site collections in SharePoint. In my case today, I had to activate "Open In Client" feature of SharePoint to disable browser based editing for all site collection in a SharePoint web application.

How to Activate a Feature in SharePoint?
To activate a site collection feature from SharePoint web UI, You can navigate to Site settings >> Site Collection Features >> Click on "Activate" next to the relevant feature.

Activate Feature on All Sites in SharePoint using PowerShell
Pretty straight forward and easy enough through the UI, isn't it? Well, If you have to do the same for a dozens of site collection in the web application, PowerShell is the perfect solution.

Activate feature for all site collections in SharePoint
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$WebAppURL="https://portal.crescent.com/"
$FeatureName="OpenInClient" #Feature Folder Name

#Get all site collections of the web app and iterate through
$SiteColl= Get-SPSite -WebApplication $WebAppURL -Limit ALL
Foreach($Site in $SiteColl)
{ 
 #Get the Feature ID from Feature Name
    $Feature = Get-SPFeature -Site $Site.Url | Where {$_.DisplayName -eq $FeatureName}
 
 #If Feature Found
 if($Feature -ne $null)
    {
       #Check if the Feature is activated already
       if (Get-SPFeature -Site $Site.Url | Where {$_.ID -eq $Feature.Id})
       {
         Write-Host $FeatureName "is already activated at " $Site.Url -f Yellow
        }
       else
       {
       #Activate the Feature
       Enable-SPFeature –identity $Feature.ID -URL $Site.URL
       Write-Host "Feature Activated on "$site.URL -f Green   
  }
 }
 else
 {
      Write-Host "Feature Name doesn't exist:"$FeatureName -f Red
 } 
}
This PowerShell script activates feature on all site collections.

You can also use this one-liner to activate feature on all site collections: (No error handling, however!)
Get-SPSite -Limit All | foreach {Enable-SPFeature "0561d315-d5db-4736-929e-26da142812c5" -url $_.URL } 

SharePoint: activate feature on all sites
Consider a scenario, where you have to activate a site level (web) feature on 100's of sites under a site collection, which is quite common. In my case, I had to activate Nintex Workflow 2013 feature on all sub sites within a particular site collection.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$SiteCollURL="http://portal.crescent.com/"
$FeatureID="0561d315-d5db-4736-929e-26da142812c5" 

#Get the Feature
$Feature = Get-SPFeature | Where {$_.ID -eq $FeatureID}

#If Feature Found
if($Feature -ne $null)
{     
    #Get all sites under the collection and loop through
    $Site= Get-SPSite $SiteCollURL
    Foreach($Web in $Site.AllWebs)
    {
  #Check if the Feature is activated already
       if (Get-SPFeature -Web $web.Url | Where {$_.ID -eq $FeatureId})
       {
         Write-Host $FeatureID "is already activated at " $Web.Url -f "Blue"
        }
       else
       {
           #Activate the Feature
           Enable-SPFeature –identity $FeatureID -URL $Web.URL
           Write-Host "Feature has been Activated on "$Web.URL -f "Green"
       }
    }
}
else    
{
    Write-Host "Feature doesn't exist:"$FeatureID -f "Red"
}
This script activates feature for all sites in SharePoint.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...