GetFeatureDefinition – A SharePoint PowerShell Function

I cant believe I have not posted this simple PowerShell function. I have been doing some packaging of features and solutions lately so many of my feature-related scripts come to the foreground.

The GetFeatureDefintion function will return either a single Feature Defintion (SPFeatureDefinition) if you pass in a Feature Id or all feature definitions ( Array of Feature Definitions) on the farm . This is a great little function to include in your profile. Before you attempt to use this function a little housekeeping is in order. First you need to run this in a PowerShell session on a server that has WSS 3 or MOSS 2007. Next, you will need to load the SharePoint assembly into the PowerShell session.

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”)

PowerShell should respond with information about the assembly.

And here is the script:

function GetFeatureDefinition
{
param($featureID=””)
$localFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
if($featureID -ne “”)
{
return $localFarm.FeatureDefinitions | where-object{$_.Id -eq $featureid}
}
else
{
return $localFarm.FeatureDefinitions
}
}

Once the function has been defined in your PowerShell session try the following commands:

GetFeatureDefinition

GetFeatureDefinition F6924D36-2FA8-4f0b-B16D-06B7250180FA

Of course you can always format the output using format commands in PowerShell:

GetFeatureDefinition | Format-Table DisplayName, Id, Scope, Hidden

GetFeatureDefinition F6924D36-2FA8-4f0b-B16D-06B7250180FA | Format-Table DisplayName, Id, Scope, Hidden

And of course you can filter and sort using supplied PowerShell commands and the pipeline:

GetFeatureDefinition | Where-Object{$_.DisplayName -like ‘Publish*’} | Sort-Object Scope | FT DisplayName, Id, Scope, Hidden

Or

GetFeatureDefinition | Where-Object{$_.Hidden -eq $True} |Sort-Object DisplayName | FT DisplayName, Id, Scope, Hidden

And don’t forget that with PowerShell you are working with objects:

(GetFeatureDefinition | Where-Object{$_.DisplayName -like ‘Publish*’} ).Count

(GetFeatureDefinition | Where-Object{$_.Hidden -eq $True}).Count

Leave a Reply