GetActiveFeatureDefinitions – A SharePoint PowerShell Function

Here is one more feature-related PowerShell script to quickly display which features are active based on a given SharePoint url. This script takes a SharePoint end point (url) as the first parameter and optional exclude parameters for each of the possible feature scopes. This function will output SPFeatureDefinition objects that will display in the PowerShell host or can be used further in the pipeline as SPFeatureDefinitions.

I want to point out that this function returns SPFeatureDefinition objects as the function name suggests. Why do I return SPFeatureDefintion objects? Simple, the SPFarm object does not contain a collection of activated features while all the other scopes (WebApplication, Site, and Web) do. There is no SPFeature to retrieve from the SPFarm, all intstalled features scoped for the farm are active. Since I use this function as a simple and quick way to see what features are activated based on the url endpoint I really only need the feature definition which contains the interesting properties including displayname, scope and hidden.

Now one warning – this function simply determines which features are activated based on the url end point. This does not mean that the feature directly affect the particular web or site. Some features are scoped at the farm or web app for a particular end point but does not manifest itself in the site or web. For example, you might find publishing features listed as active when you are looking at a team site. Even though the team site is not using publishing features the feature is still activated for the farm or web application. For this reason I included parameters that allow you to exclude features scoped at a particular level.

To use this function you must load the Microsoft.SharePoint assembly into your PowerShell host. To load the SharePoint Assembly in a PowerShell session type and run the following command:

[Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

And now the function:

function GetActiveFeatureDefinitions
{
param ([String] $webUrl,
[Switch] $ExcludeFarm,
[Switch] $ExcludeWebApp,
[Switch] $ExcludeSite,
[Switch] $ExcludeWeb )

$site = new-object Microsoft.SharePoint.SPSite $webUrl
$web = $site.OpenWeb()
$webAPP = $site.WebApplication
$Farm = $WebApp.Farm

if(!$ExcludeWeb.IsPresent)
{ $featureDefs = $web.features | %{$_.Definition}}

if(!$ExcludeSite.IsPresent)
{ $featureDefs += $site.features | %{$_.Definition}}

if(!$ExcludeWebApp.IsPresent)
{ $featureDefs += $webApp.features | foreach-object{$_.Definition}}

if(!$ExcludeFarm.IsPresent)
{ $featureDefs += $farm.FeatureDefinitions | Where-Object{$_.Scope -eq ‘Farm’}}

$web.Dispose
$site.Dispose
return $featureDefs
}

Here are some example uses of GetActiveFeatureDefintions:

GetActiveFeatureDefinitions http://localhost
GetActiveFeatureDefinitions http://localhost -excludeFarm
GetActiveFeatureDefinitions http://localhost -excludeFarm -excludeWebApp

Remember the function exports SPFeatureDefinition objects and can be used in the pipeline. Here are a few examples to try:

$fds= GetActiveFeatureDefinitions http://localhost -excludeFarm
$fds.count

GetActiveFeatureDefinitions http://localhost -excludeFarm | Sort-Object Scope | Format-Table DisplayName, Scope, Hidden, Id -auto

GetActiveFeatureDefinitions http://localhost | Where-Object{$_.Hidden -eq $true} |Sort-Object Scope | Format-Table DisplayName, Scope, Hidden, Id -auto

Leave a Reply