2015-10-09

When you upgrade to a newer version of SharePoint, you will no doubt find issues with SharePoint Features that aren’t in the newer Farm. This results in one or more orphaned features. You can either deploy the Features to the new Farm, or you can remove them with this script:

$results = @()

foreach($site in Get-SPSite -limit all) {
    #write-host "Site : " $site.URL
    foreach ($feature in $site.features) {
        $obj = New-Object PSObject
    
       if ($feature.definition -eq $null) {
            $obj | Add-Member NoteProperty "Site/Web Title"($site.Title)
            $obj | Add-Member NoteProperty "Site/Web URL"($site.URL)
            $obj | Add-Member NoteProperty "Feature ID"($feature.DefinitionId)
            $results += $obj
            
            $site.features.remove($feature.DefinitionId,$true)
        }
    }
    $webs = $site | get-spweb -limit all
    foreach ($web in $webs) { 
    
        foreach ($feature in $web.features) {
            $obj = New-Object PSObject
            if ($feature.definition -eq $null) {

                $obj | Add-Member NoteProperty "Site/Web Title"($web.Title)
                $obj | Add-Member NoteProperty "Site/Web URL"($web.URL)
                $obj | Add-Member NoteProperty "Feature ID"($feature.DefinitionId)
                $results += $obj
                
                $web.Features.Remove($feature.DefinitionId, $true)
            }
        }
        $web.dispose()
    }
    $site.dispose()
}

$results | Export-Csv "C:\MissingFeatures.csv" -notype

Running the Script above will remove all of the missing Features from all of the Site Collections as well as each Sub Site. There’s also some code to log the progress.

Note: Sometime’s you may get the following error:

Exception calling "Remove" with "2" argument(s): "Attempted to perform an unauthorized operation."

If you do receive this error, then it could be down to publishing pages (with versioning enable). I did some research and found something interesting saying that, If we add a feature on the publishing page with versioning enabled on and after we remove that feature from that, it will create new version of the page. Therefore the previous version of that page contains that feature. So to remove the feature we have to remove the version history where the feature was applied.

 

About the author 

Rahul Gokani