Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am developing a script to refresh my custom features. Earlier i was just looping through the features and calling Disable-SPFeature and Enable-SPFeature on each items.

The issue is when the feature is not activated. I would like to check if feature is already activated on the scope and taking appropriate action based on the feature status.

any help, pointers

Sudhir

share|improve this question

4 Answers 4

up vote 18 down vote accepted

To check the state of a feature use

(Get-SPFeature -Identity <FEATURE> -ErrorAction SilentlyContinue -Farm/-WebApplication/ -Site/-Web ) -ne $null

The extra -Scope parameter will check if the feature is active at that scope.

See MSDN Get-SPFeature

If you want to activate the feature then a try/catch trap block can be used

try
{
    Enable-SPFeature -Identity <FEATURE> -Url <SCOPE> -ErrorAction Stop
}
catch [System.Management.Automation.ActionPreferenceStopException]
{
    if( !($_.Exception -is [System.Data.DuplicateNameException]) )
    {
        #Its not a "feature is already activated at scope" exception
        throw    
    }
    else
    {
        #Handle the "feature is already activated at scope" exception
    }
}

But as you can see it's not pretty.

share|improve this answer
    
+1 Not pretty like many things in this with SharePoint and PowerShell but you can get them to work, this is the method we use for deploying WSP's with PowerShell and making sure things are active. –  MichaelF May 19 '11 at 20:29
    
We chose the catch approach as we weren't happy with -ErrorAction SilentlyContinue it would silently swallow feature activation exceptions and other unexpected errors. –  Steve P May 20 '11 at 5:54
    
Hey Thanks for your answer:- "(Get-SPFeature -Identity <FEATURE> -ErrorAction SilentlyContinue -Farm/-WebApplication/ -Site/-Web <URL>) -ne $null" this seems to be doing the trick. –  SharePoint xPert May 21 '11 at 11:47

http://soreddymanjunath.blogspot.in/2014/06/powershell-script-to-check-for-feature.html

Here is the simple script to check whether feature is activated or not in all the sitecollections in a Webapplication, if the feature is not active go and enable the feature. Here we are using feature id to check for feature.

clear

Add-PSSnapin "Microsoft.SharePoint.Powershell" 

try
{
$web=Get-SPWebApplication "http://SP2013LocalDev/" | Get-spsite -Limit All | get-spweb -Limit All | % {

$bool=Get-SPFeature -Web $_ | where {$_.Id -eq "54944497-1d5b-443d-aba9-d20991ed18bb"}

                if(!$bool)
                {
                Enable-SPFeature -Identity "54944497-1d5b-443d-aba9-d20991ed18bb" -Url $_.Url
                Write-Host  "Feature Activated"
                }
      else
      {
      Write-Host "Feature is Active at" $_.Url
      }

   }

}
catch{
         $ErrorMessage = $_.Exception.Message
         Write-Host $web.Title  $ErrorMessage
    }
share|improve this answer
function Check-SPFeatureActivated
{
    param([string]$Id=$(throw "-Id parameter is required!"),
            [Microsoft.SharePoint.SPFeatureScope]$Scope=$(throw "-Scope parameter is required!"),
            [string]$Url)  
    if($Scope -ne "Farm" -and [string]::IsNullOrEmpty($Url))
    {
        throw "-Url parameter is required for scopes WebApplication,Site and Web"
    }
    $feature=$null

    switch($Scope)
    {
        "Farm" { $feature=Get-SPFeature $Id -Farm }
        "WebApplication" { $feature=Get-SPFeature $Id -WebApplication $Url }
        "Site" { $feature=Get-SPFeature $Id -Site $Url }
        "Web" { $feature=Get-SPFeature $Id -Web $Url }
    }
    #return if feature found or not (activated at scope) in the pipeline
    $feature -ne $null
}

Check-SPFeatureActivated -Id 00bfea71-1e1d-4562-b56a-f05371bb0115 -Scope "Web" -Url "http://mysharepointsite/"
share|improve this answer
    
this will throw exceptions if the feature is not active. I think that if the scenario assume that the GUID is always valid (e.g. it was retrieved from another cmdlet against the farm) you can safely add -ErrorAction SilentlyContinue to the Get-SPFeature cmdlet, you will avoid exceptions and it assumes that4 when it errors out the feature is not active –  Thiago Silva Nov 5 '13 at 16:03

You can use SharePoint object model from PowerShell to achieve this:

PS C:\Users\omlin> $featureGuid = "PUT-GUID-HERE"
PS C:\Users\omlin> $web = Get-SPWeb http://localhost
PS C:\Users\omlin> $feature = $web.Features[$featureGuid]
PS C:\Users\omlin> if ($feature -eq $null) { "does not activated" } else { "activated" }
does not activated
PS C:\Users\omlin>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.