How to Remove Orphaned Features from SharePoint 2013 Farm

Posted By Posted by: Peter Williams on June 13, 2015

After performing a health check on a SharePoint 2013 farm recently, I decided it was time to resolve an issue that had been lingering for some time. In our multi-tenant farm, there were several sites that had orphaned features and some of these features were triggering event log errors frequently.

I had been putting this task off for a while as I didn’t have any experience with removal of orphaned features. Fortunately, the task was not too onerous due to a handy tool I found and a script that was provided to me by a colleague.

The script found at the end of this blog cycles through farm features, web app features, site collection features and then web features and outputs the results to a text file. I ran this script on a web application by web application basis. The output file then helped determine if the web app contained orphaned features or not.

I then found a tool called FeatureAdmin as referenced here. http://featureadmin.codeplex.com/. What a great tool! It could not have made the process to remove orphaned features any easier. The site explains how to use the tool well and also explains the permissions required to operate it.

After using the tool to remove the orphaned features, I ran the script below to double check that all orphaned features were indeed removed. I was able to clean up the entire farm that contained 17 web applications in less than two hours.

 

List Orphaned Features Script

Add-PsSnapin Microsoft.SharePoint.PowerShell

 

## SharePoint DLL

[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

$webApplicationURL = “https://entertargetURLhere”

set-variable -name out -value “C:\Tools\MissingFeatures.csv”

# Farm features

$farmFeatures = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService.Features

foreach ($feature in $farmFeatures) {

$feature

if ($feature.definition -eq $null) {

“Missing farm feature: ” + $feature.DefinitionId + ” in ” + $feature.parent | Out-File $out -Append

write-host “Missing farm feature:”

write-host $feature.DefinitionId

write-host $feature.parent

# $farmFeatures.remove($featureid)

}

else

{

$feature.defintion.Name

}

}

# Web app features

$webApp = Get-SPWebApplication $webApplicationURL

 

if($webApp -ne $null)

{

“Web Application : ” + $webApp.Name | Out-File $out -Append

 

foreach ($feature in $webApp.features) {

 

if ($feature.definition -eq $null) {

“Missing web app feature: ” + $feature.DefinitionId + ” in ” + $feature.parent | Out-File $out -Append

 

write-host “Missing web app feature:”

write-host $feature.DefinitionId

write-host $feature.parent

# $webApp.features.remove($featureid)

}

}

# Site collection and web features

foreach($siteColl in $webApp.Sites)

{

if($siteColl -ne $null)

{

$siteColl.Url | Out-File $out -Append

 

## get the missing site collection features

foreach ($feature in $siteColl.features) {

if ($feature.definition -eq $null)

{

“Missing site feature: ” + $feature.DefinitionId + ” in ” + $feature.parent | Out-File $out -Append

write-host “Missing site feature:”

write-host $feature.DefinitionId

write-host $feature.parent

# $site.features.remove($featureid)

}

}

 

$webs = $siteColl | get-spweb -limit all

foreach ($web in $webs)

{

 

foreach ($feature in $web.features)

{

if ($feature.definition -eq $null)

{

“Missing web feature: ” + $feature.DefinitionId + ” in ” + $feature.parent | Out-File $out -Append

write-host “Missing web feature:”

write-host $web.url

write-host $feature.DefinitionId

write-host $feature.parent

# $web.features.remove($featureid)

}

}

}

$siteColl.Dispose()

}

else

{

Echo $siteColl “does not exist”

}

}

}

else

{

Echo $webApplicationURL “does not exist, check the WebApplication name”

}

 

Remove-PsSnapin Microsoft.SharePoint.PowerShell

 

Echo Finish


Blog Posted In 

Leave a Reply

Your email address will not be published.