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:

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. [Parameter(Mandatory=$false, Position=1)]
  7. [String]$Url,
  8. [Parameter(Mandatory=$true, Position=2)]
  9. [ValidateNotNullOrEmpty()]
  10. [String]$FeatureTitle
  11. )
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. #Filtering site collections based on whether they are read-only, read-locked or Write-Locked. Such sites are excluded.
  14. $sites = $sites | Where-Object{$_.ReadOnly -ne $null -and $_.ReadLocked -ne $null -and $_.WriteLocked -ne $null}
  15. if($sites -ne $null)
  16. {
  17. foreach($site in $sites)
  18. {
  19. try
  20. {
  21. $webList += $site.AllWebs
  22. }
  23. catch
  24. {
  25. Write-Host -ForegroundColor Yellow "Site:"$site.Url
  26. Write-Host -ForegroundColor Red "Error:"$_.Exception.Message
  27. Start-Sleep -Seconds 5
  28. }
  29. $site.Dispose()
  30. }
  31. }
  32. if($Scope -eq "Site")
  33. {
  34. $webs = (Get-SPSite -Identity $url).AllWebs
  35. $webList += $webs
  36. }
  37. if($Scope -eq "Web")
  38. {
  39. $webList = Get-SPWeb -Identity $Url -ErrorAction Stop
  40. }
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. Write-Host " Disabling feature "$targetFeature.Definition.GetTitle(1033)
  13. try
  14. {
  15. #Disable feature
  16. Disable-SPFeature -Identity $targetFeature -Url $web.Url -ErrorAction Stop -Confirm:$false
  17. Write-Host -ForegroundColor Green " Feature "$targetFeature.Definition.GetTitle(1033) " disabled successfully."
  18. }
  19. catch
  20. {
  21. Write-Host -ForegroundColor Red "Error disabling feature("$targetFeature.Definition.GetTitle(1033) "):"$_.Exception.Message
  22. }
  23. }
  24. else
  25. {
  26. Write-Host -ForegroundColor Yellow " Feature not found with specified name:"$FeatureTitle
  27. }
  28. $web.Dispose()
  29. }
  30. }
Finally it disposes all the site objects (SPWeb).

The complete script is attached to this article.