Disable SharePoint Site Feature On Multiple Sites

Recently we did SharePoint 2013 to SharePoint 2013 migration from one domain to other domain. We did this migration with ‘Database Attach’ approach. During this migration, we have one of the task to complete is to disable particular feature from all webs within a web application. There were hundreds of sites within that web application and disabling the feature one by one would have taken days to complete that activity. So I created PowerShell script which does this within very less time.

Pre-requisite:

  • Run this script on SharePoint Central Administration Server as Farm Administrator.
  • Name of the feature which needs to be disabled.
  • URL of target Web Application or Site Collection or Site (SPWeb).

How it works:

I created a generic script which takes three input parameters. First input parameter is $Scope, which takes values as Farm/WebApplication/Site/Web. Second parameter it takes is $Url which is required if you are running the script for Web Application/SiteCollection/Web. If you running this script on entire farm, you don’t need to specify value for this parameter as this is optional. The third parameter is $FeatureTitle which is mandatory.

  1. Param  
  2. (  
  3.    [Parameter(Mandatory=$true, Position=0)]  
  4.    [ValidateSet("Farm","WebApplication","Site","Web")]      
  5.    [String]$Scope,  
  6.      
  7.     [Parameter(Mandatory=$false, Position=1)]  
  8.     [String]$Url,  
  9.   
  10.     [Parameter(Mandatory=$true, Position=2)]  
  11.     [ValidateNotNullOrEmpty()]  
  12.     [String]$FeatureTitle  
  13.   
  14. )   
Based on $Scope, it processes to find out list of site (SPWeb) objects. If $Scope if Farm or WebApplication, it finds all the site collections within that Farm or WebApplication. Then it finds all the sites (SPWeb) from the site collection set.
  1. if($Scope -ne $null -and $Url -ne $null)  
  2. {  
  3.    if($Scope -eq "Farm")  
  4.    {  
  5.         #Finding all site collection within Farm  
  6.         $sites = Get-SPSite -Limit All -ErrorAction Stop  
  7.    }   
  8.    if($Scope -eq "WebApplication")  
  9.    {  
  10.      #Finding all site collection with particular Web Application.     
  11.      $sites = Get-SPSite -WebApplication $Url -Limit All -ErrorAction Stop  
  12.        
  13.    }  
  14.    #Filtering site collections based on whether they are read-only, read-locked or Write-Locked. Such sites are excluded.  
  15.    $sites = $sites | Where-Object{$_.ReadOnly -ne $null -and $_.ReadLocked -ne $null -and $_.WriteLocked -ne $null}    
  16.    if($sites -ne $null)  
  17.     {  
  18.         foreach($site in $sites)  
  19.         {  
  20.              
  21.             try  
  22.             {      
  23.                 $webList += $site.AllWebs  
  24.             }  
  25.             catch  
  26.             {  
  27.                 Write-Host -ForegroundColor Yellow "Site:"$site.Url  
  28.                 Write-Host -ForegroundColor Red "Error:"$_.Exception.Message  
  29.                 Start-Sleep -Seconds 5  
  30.             }  
  31.             $site.Dispose()  
  32.         }  
  33.     }   
  34.    if($Scope -eq "Site")  
  35.    {  
  36.         $webs = (Get-SPSite -Identity $url).AllWebs  
  37.         $webList += $webs    
  38.    }  
  39.    if($Scope -eq "Web")  
  40.    {  
  41.         $webList = Get-SPWeb -Identity $Url -ErrorAction Stop  
  42.    }   
Once we have list of all the sites (SPWeb), then it iterate through each site and find the target feature based on supplied $FeatureTitle. If it finds feature with that title, it disables it.
  1. if($webList -ne $null)  
  2. {  
  3.    foreach($web in $webList)  
  4.    {  
  5.      Write-Host "Processing web:"$web.Url  
  6.      $targetFeature = $null  
  7.      $targetFeature = $web.Features | ?{$_.Definition.GetTitle(1033) -eq $FeatureTitle}  
  8.      #Get-SPFeature -Web $web -Limit All |  select DisplayName, {$_.GetTitle(1033)}, Hidden, Status  
  9.      #$web.Features | ?{$_.Definition.Hidden -eq $false} | select {$_.Definition.GetTitle(1033)},{$_.Definition.DisplayName.ToString()},{$_.Definition.Status}, {$_.Definition.Hidden}  
  10.      if($targetFeature -ne $null)  
  11.      {  
  12.            
  13.          Write-Host " Disabling feature "$targetFeature.Definition.GetTitle(1033)  
  14.          try  
  15.          {  
  16.              #Disable feature  
  17.              Disable-SPFeature -Identity $targetFeature -Url $web.Url -ErrorAction Stop -Confirm:$false  
  18.              Write-Host -ForegroundColor Green " Feature "$targetFeature.Definition.GetTitle(1033) " disabled successfully."  
  19.          }  
  20.          catch  
  21.          {  
  22.              Write-Host -ForegroundColor Red "Error disabling feature("$targetFeature.Definition.GetTitle(1033) "):"$_.Exception.Message  
  23.          }  
  24.            
  25.      }  
  26.      else  
  27.      {  
  28.          Write-Host -ForegroundColor Yellow " Feature not found with specified name:"$FeatureTitle  
  29.      }  
  30.      $web.Dispose()  
  31.    }    
  32.        
  33. }   
Finally it disposes all the site objects (SPWeb).

The complete script is attached to this article.