1

I want to activate a feature in all web app in a farm. I need to chek a condition if the feature is deactivate dthen only it should get activated? Suggestions are welcome.

  • Y i need the condition to acitvate only deactivated feature is because am getting error like that feature is already activated. so to avoid that need to write a condition. – priya Jan 2 '14 at 6:32
1

To see if a feature is activated in a site collection, you can use follwoing powershell code:

$IsActiveFeature = Get-SPFeature -Identity $FeatureID -Site $SiteUrl -ErrorAction SilentlyContinue

The feature can be deployed at any scope like Farm/Web Application/Site Collection/Web.. You need to provide different parameters based on that as below:

function Deactivate-SPFeature
{
    param ($FeatureID, $SiteUrl, $WebApplicationUrl)
    $Feature = Get-SPFeature -Identity $FeatureID -ErrorAction SilentlyContinue
    $IsActiveFeature
    If ($Feature -eq $null)
    {
        Write-Warning "The specified feature ($FeatureID) was not found."
        return
    }
    If ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Farm)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Farm -ErrorAction SilentlyContinue
        If ($IsActiveFeature -eq $null)
        {
            Write-Warning "The specified feature ($FeatureID) is not activated on the Farm."
            return;
        }
        Write-Host "Deactivating Feature $FeatureID."
        Disable-SPFeature -Identity $FeatureID  -confirm:$false
        Write-Host "Feature  $FeatureID deactivated sucessfully"  -foregroundcolor Green
        return;
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::WebApplication)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -WebApplication $WebApplicationUrl -ErrorAction SilentlyContinue
        If ($IsActiveFeature -eq $null)
        {
            Write-Warning "The specified feature ($FeatureID) is not activated in the Web application ($WebApplicationUrl)."
            return;
        }
        Write-Host "Deactivating Feature $FeatureID."
        Disable-SPFeature -Identity $FeatureID -Url $WebApplicationUrl -confirm:$false
        Write-Host "Feature  $FeatureID deactivated sucessfully"  -foregroundcolor Green
        return;
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Site)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Site $SiteUrl -ErrorAction SilentlyContinue
    }
    ElseIf ($Feature.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Web)
    {
        $IsActiveFeature = Get-SPFeature -Identity $FeatureID -Web $SiteUrl -ErrorAction SilentlyContinue
    }
    If ($IsActiveFeature -eq $null)
    {
        Write-Warning "The feature $FeatureID is not activated on the site $SiteUrl"
        return;
    }

    Write-Host "Deactivating Feature $FeatureID."
    Disable-SPFeature -Identity $FeatureID  -Url $SiteUrl -confirm:$false
    Write-Host "Feature  $FeatureID deactivated sucessfully"  -foregroundcolor Green
}

Please refer my blog post for full information:

http://sharepointnadeem.blogspot.in/2011/08/sharepoint-2010-powershell-deployment.html

  • thanks for the reply.now i need to activate the feature in all webapp except for one particular webapp by hardcoding the webapp url.am using the below code:Add-PSSnapin Microsoft.SharePoint.PowerShell $FeatureID = Get-SPFeature a44d2aa3-affc-4d58-8db4-f4a3af053188if($IsActiveFeature = Get-SPFeature -Identity $FeatureID -Site $SiteUrl -ErrorAction SilentlyContinue) {Get-SPSite -Limit ALL | % {Enable-SPFeature $FeatureID -Url $_.Url} } if($webApplications = Get-SPWebApplication cloudx-151-145:22222) {Get-SPSite -Limit ALL | % {Disable-SPFeature $FeatureID -Url $_.Url}} } – priya Jan 2 '14 at 7:12
  • Powershell uses operators like -eq, -ne and not something like = – Nadeem Yousuf Jan 2 '14 at 7:22
0

You can use following command to activate feature

Enable-SPFeature -Identity "feature id" -url "url"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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