Wednesday, November 21, 2012

PowerShell check if SharePoint feature is activated at farm scope

Scenario: Most of the times when you ship solution package you deliver PowerShell installation scripts, part of which would be activating the features. But before activating you may want to check if its already activated at a given scope. And if activated already skip it. How to preform this check?

$featureGuid = "11075dtfg-cb48-48a0-8b21-001ac2d"

$feature=(Get-SPFeature -Identity $featureGuid -ErrorAction SilentlyContinue -Farm) -ne $null

if($feature -ne $true)
  #feature is not activated at the given scope so activate it

In the above you can replace "-Farm" with any of the other valid scopes.

Other way to do this and only works for Webapp,Site, and web scoped features.
$webApp = read-host "Enter the URL of the SharePoint Web app"    
$webApp2 = Get-SPWebApplication $webApp
$UpdateFeature = $webApp2.Features[$UpdateFeatureGuid]

if($UpdateFeature -eq $null)
#Feature is currently deactivated,Activate Feature #Update