19

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

4 Answers 4

31

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.

3
  • +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
    Commented May 19, 2011 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
    Commented May 20, 2011 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. Commented May 21, 2011 at 11:47
8

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>
7
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/"
1
  • 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 Commented Nov 5, 2013 at 16:03
1

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
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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