PowerShell Script To Find Where SharePoint 2007 Features Are Activated

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
This article is part of the GWB Archives. Original Author: Brian T. Jackett

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.