PowerShell Script To Find Where SharePoint 2007 Features Are Activated
- by Brian T. Jackett
Recently I posted a script to find where SharePoint 2010 Features Are Activated.  I built the original version to use SharePoint 2010 PowerShell commandlets as that saved me a number of steps for filtering and gathering features at each level.  If there was ever demand for a 2007 version I could modify the script to handle that by using the object model instead of commandlets.  Just the other week a fellow SharePoint PFE Jason Gallicchio had a customer asking about a version for SharePoint 2007.  With a little bit of work I was able to convert the script to work against SharePoint 2007.     Solution     Below is the converted script that works against a SharePoint 2007 farm.  Note: There appears to be a bug with the 2007 version that does not give accurate results against a SharePoint 2010 farm.  I ran the 2007 version against a 2010 farm and got fewer results than my 2010 version of the script.  Discussing with some fellow PFEs I think the discrepancy may be due to sandboxed features, a new concept in SharePoint 2010.  I have not had enough time to test or confirm.  For the time being only use the 2007 version script against SharePoint 2007 farms and the 2010 version against SharePoint 2010 farms.     Note: This script is not optimized for medium to large farms.  In my testing it took 1-3 minutes to recurse through my demo environment.  This script is provided as-is with no warranty.  Run this in a smaller dev / test environment first.                                  001                  002                   003                   004                   005                   006                   007                   008                   009                   010                   011                   012                   013                   014                   015                   016                   017                   018                   019                   020                   021                   022                   023                   024                   025                   026                   027                   028                   029                   030                   031                   032                   033                   034                   035                   036                   037                   038                   039                   040                   041                   042                   043                   044                   045                   046                   047                   048                   049                   050                   051                   052                   053                   054                   055                   056                   057                   058                   059                   060                   061                   062                   063                   064                   065                   066                   067                   068                   069                   070                                             function Get-SPFeatureActivated                   {                   # see full script for help info, removed for formatting                  [CmdletBinding()]                   param(                         [Parameter(position = 1, valueFromPipeline=$true)]                       [string]                       $Identity                   )#end param                       Begin                       {                             # load SharePoint assembly to access object model                           [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")                                                     # declare empty array to hold results. Will add custom member for Url to show where activated at on objects returned from Get-SPFeature.                           $results = @()                                                         $params = @{}                         }                         Process                       {                             if([string]::IsNullOrEmpty($Identity) -eq $false)                             {                                 $params = @{Identity = $Identity}                             }                                                         # create hashtable of farm features to lookup definition ids later                           $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local                                                                 # check farm features                           $results += ($farm.FeatureDefinitions | Where-Object {$_.Scope -eq "Farm"} | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} |                                            % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value ([string]::Empty) -PassThru} |                                            Select-Object -Property Scope, DisplayName, Id, Url)                                                         # check web application features                           $contentWebAppServices = $farm.services | ? {$_.typename -like "Windows SharePoint Services Web Application"}                                                         foreach($webApp in $contentWebAppServices.WebApplications)                             {                                 $results += ($webApp.Features | Select-Object -ExpandProperty Definition | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} |                                            % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $webApp.GetResponseUri(0).AbsoluteUri -PassThru} |                                            Select-Object -Property Scope, DisplayName, Id, Url)                                                                 # check site collection features in current web app                               foreach($site in ($webApp.Sites))                                 {                                     $results += ($site.Features | Select-Object -ExpandProperty Definition | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} |                                                    % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $site.Url -PassThru} |                                                    Select-Object -Property Scope, DisplayName, Id, Url)                                                                         # check site features in current site collection                                   foreach($web in ($site.AllWebs))                                     {                                         $results += ($web.Features | Select-Object -ExpandProperty Definition | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} |                                                        % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $web.Url -PassThru} |                                                        Select-Object -Property Scope, DisplayName, Id, Url)                                                                                                $web.Dispose()                                     }                                     $site.Dispose()                                 }                             }                         }                         End                       {                             $results                       }                     } #end Get-SPFeatureActivated                                     Get-SPFeatureActivated                            Conclusion     I have posted this script to the TechNet Script Repository (click here).  As always I appreciate any feedback on scripts.  If anyone is motivated to run this 2007 version script against a SharePoint 2010 to see if they find any differences in number of features reported versus what they get with the 2010 version script I’d love to hear from you.           -Frog Out