Sunday, March 23, 2014

Activate-Deactivate Feature on All Sites,Site Collections in SharePoint using PowerShell

We use "Enable-SPFeature" and " Disable-SPFeature" PowerShell cmdlets in SharePoint 2010 / SharePoint 2013 sites to activate or deactivate features as in my another article: How to Activate-Deactivate Features in SharePoint. At times, we may have to activate feature for all site collections or deactivate feature on all sites. Here is my PowerShell scripts to do that.

Enable a feature for all site collections in SharePoint:
In my case, I had to enable "Open Documents in Client Applications" feature in every site collection under a web application to force opening documents in Office clients such as Microsoft Word instead of Office web apps due the licensing limitation.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#$WebAppURL = ""
$FeatureName ="OpenInClient"

#Get the Web Application
$webApp = Get-SPWebApplication -Identity $WebAppURL

#Get all site collections with in the web app
$SitesCollection = $WebApp | Get-SPSite -limit all 

#Iterate through each site 
foreach ($site in $SitesCollection)
    #Check if feature is already activated
    $feature = Get-SPFeature -site $site.Url  | Where-object {$_.DisplayName -eq $FeatureName}
    if($feature -eq $null)
        #Enable the feature 
        Enable-SPFeature -Identity $FeatureName -url $site.url -Confirm:$False
        Write-host "Feature Activated on $($site.URL)" -ForegroundColor Green    
        Write-host "Feature is already Active on $($site.URL)" -ForegroundColor Red

PowerShell to Deactivate Feature on All Sites:
Requirement: Deactivate "Mobile Redirect" feature on all sites in a SharePoint 2010 web application.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
$WebAppURL = "" 
$FeatureName = "MobilityRedirect"
#Disable Mobile View feature on all sites in the web application
$WebsCollection = Get-SPWebApplication $WebAppURL | Get-SPSite -Limit ALL | Get-SPWeb -Limit ALL
#Itereate through each web
ForEach($Web in $WebsCollection)
    #Check if feature is already activated
    $feature = Get-SPFeature -web $Web.Url  | Where-object {$_.DisplayName -eq $FeatureName}
    if($feature -ne $null)
        #Disable the Mobile browser view feature
        Disable-SPFeature -identity $FeatureName -URL $Web.URL -Force -Confirm:$false
        write-host "Mobile Redirect feature deactivated at site: $($Web.Url)"

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

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...