Activate-Deactivate Feature on All Site Collections in SharePoint using PowerShell
We use “Enable-SPFeature” and ” Disable-SPFeature” PowerShell cmdlets in SharePoint 2013 / SharePoint 2016 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 the “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 to the licensing limitation.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#variables
#$WebAppURL = "https://intranet.crescent.com"
$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
}
else
{
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 2013 web application.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Variables
$WebAppURL = "https://intranet.crescent.com"
$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)"
}
}