MOSS2007: How to Remove Orphaned Features Using PowerShell

MOSS2007: How to Remove Orphaned Features Using PowerShell

I recently had to use content deployment on some old MOSS 2007 systems. These systems are alive for a long time and a lot of solutions/features have been deployed to them. 

If features are not deactived before removal they stay as an orphan in the system (contentdb) and can cause issues lateron. I had to cleanup orphaned features before the content deployment worked properly. 

The procedure for identyfing and removing orphaned features is quite straightforward:
  1. Go through every site collection and site recursively
  2. For each SPWebApp, SPSite and SPWeb
  3. Iterate through the *.Features collection
  4. For each feature, if the definition is null the script removes it from the feature collection 

** Use this at your own risk **

01.[system.reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint")
02. 
03.function ListFeaturesAux($header, $features, $obj, $boolremove)
04.{
05.    foreach ($feature in $features)
06.    {
07.        if( $feature.Definition -eq $NULL)
08.        {
09.            write-host "Missing feature definition (feature id:" $feature.DefinitionId  "), Scope: "  $header
10.            if( $boolremove -eq $TRUE)
11.            {
12.                try {
13.                    $obj.Features.Remove($feature.DefinitionId,$true)
14.                    write-host "Feature "  $feature.DefinitionId " has been removed."
15. 
16.                }
17.                        catch {
18.                            write-host "An error occured while removing feature:"  $feature.DefinitionId
19.                        }
20.            }  
21.             
22.        }
23.        ELSE
24.        {
25.            #write-host $feature.Parent  " " $feature.Definition
26.        }  
27.    }
28.}
29. 
30.function ListFeaturesWeb($web, $boolremove)
31.{
32.     
33.    $header = "Web: " +  $web.Url
34.    ListFeaturesAux $header $web.Features $web $boolremove
35.     
36.    foreach( $subweb in $web.Webs)
37.    {
38.        ListFeaturesWeb $subweb $boolremove
39.    }
40.     
41. 
42.}
43. 
44.function ListFeaturesSite($site, $boolremove)
45.{
46.    $header = "Site collection: " +  $site.Url
47.    ListFeaturesAux $header  $site.Features $site $boolremove
48.             
49. 
50.}
51. 
52.function list-orphaned-Features ($url,$boolremove)
53.{
54. 
55. 
56.    $webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url)
57.    write-host $webapp.displayname
58.     
59.    foreach($Site  in $webapp.Sites )
60.    {
61.        ListFeaturesSite $Site $boolremove
62.        ListFeaturesWeb $Site.RootWeb  $boolremove
63.    }
64.}
65. 
66.#change parameter boolremove from $FALSE to $TRUE to remove the feature
67.list-orphaned-Features "http://sp2007"  $FALSE

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 3. Comment: Modified title casing, removed tag "SharePoint 2007 (en-US)", added tag "Has Code"

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Richard Mueller edited Revision 3. Comment: Modified title casing, removed tag "SharePoint 2007 (en-US)", added tag "Has Code"

Page 1 of 1 (1 items)