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.
- Param
- (
- [Parameter(Mandatory=$true, Position=0)]
- [ValidateSet("Farm","WebApplication","Site","Web")]
- [String]$Scope,
-
- [Parameter(Mandatory=$false, Position=1)]
- [String]$Url,
-
- [Parameter(Mandatory=$true, Position=2)]
- [ValidateNotNullOrEmpty()]
- [String]$FeatureTitle
-
- )
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.
- if($Scope -ne $null -and $Url -ne $null)
- {
- if($Scope -eq "Farm")
- {
- #Finding all site collection within Farm
- $sites = Get-SPSite -Limit All -ErrorAction Stop
- }
- if($Scope -eq "WebApplication")
- {
- #Finding all site collection with particular Web Application.
- $sites = Get-SPSite -WebApplication $Url -Limit All -ErrorAction Stop
-
- }
- #Filtering site collections based on whether they are read-only, read-locked or Write-Locked. Such sites are excluded.
- $sites = $sites | Where-Object{$_.ReadOnly -ne $null -and $_.ReadLocked -ne $null -and $_.WriteLocked -ne $null}
- if($sites -ne $null)
- {
- foreach($site in $sites)
- {
-
- try
- {
- $webList += $site.AllWebs
- }
- catch
- {
- Write-Host -ForegroundColor Yellow "Site:"$site.Url
- Write-Host -ForegroundColor Red "Error:"$_.Exception.Message
- Start-Sleep -Seconds 5
- }
- $site.Dispose()
- }
- }
- if($Scope -eq "Site")
- {
- $webs = (Get-SPSite -Identity $url).AllWebs
- $webList += $webs
- }
- if($Scope -eq "Web")
- {
- $webList = Get-SPWeb -Identity $Url -ErrorAction Stop
- }
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.
- if($webList -ne $null)
- {
- foreach($web in $webList)
- {
- Write-Host "Processing web:"$web.Url
- $targetFeature = $null
- $targetFeature = $web.Features | ?{$_.Definition.GetTitle(1033) -eq $FeatureTitle}
- #Get-SPFeature -Web $web -Limit All | select DisplayName, {$_.GetTitle(1033)}, Hidden, Status
- #$web.Features | ?{$_.Definition.Hidden -eq $false} | select {$_.Definition.GetTitle(1033)},{$_.Definition.DisplayName.ToString()},{$_.Definition.Status}, {$_.Definition.Hidden}
- if($targetFeature -ne $null)
- {
-
- Write-Host " Disabling feature "$targetFeature.Definition.GetTitle(1033)
- try
- {
- #Disable feature
- Disable-SPFeature -Identity $targetFeature -Url $web.Url -ErrorAction Stop -Confirm:$false
- Write-Host -ForegroundColor Green " Feature "$targetFeature.Definition.GetTitle(1033) " disabled successfully."
- }
- catch
- {
- Write-Host -ForegroundColor Red "Error disabling feature("$targetFeature.Definition.GetTitle(1033) "):"$_.Exception.Message
- }
-
- }
- else
- {
- Write-Host -ForegroundColor Yellow " Feature not found with specified name:"$FeatureTitle
- }
- $web.Dispose()
- }
-
- }
Finally it disposes all the site objects (SPWeb).
The complete script is attached to this article.