This script may save the day if you have a feature that is activated across many site collections and there happens to be an update to the feature requiring re-activation to have updates occur. The below script will loop through all sites within a web application and will reactivate (feature must already be turned on) the feature with matching ID.

Credit for feature activation check goes to Raevean

Note: Code will throw errors if the feature is not activated, but does not impact logic. Can add -ErrorAction SilentlyContinue or | out-null if this is a concern.

$webAppUrl = "http://yourWebAppUrl/"

#Ensure that powershell version is 2.0+, if so set all commands to be run on the same thread.
$ver = $host | select version
if($Ver.version.major -gt 1) 
    {$Host.Runspace.ThreadOptions = "ReuseThread"}

#Load Powershell Sharepoint Snap-in
if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0))
{
    Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell"
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

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
}

$SPWebApp = Get-SPWebApplication $webAppUrl

foreach ($SPSite in $SPWebApp.Sites)
{
    if ($SPSite -ne $null)
    {
        $SPSite.Url

        if(Check-SPFeatureActivated -Id d872c0d7-0c33-4ec0-a65f-86ee460be775 -Scope "Site" -Url $SPSite.Url){
            #deactivate
            Disable-SPFeature -identity "d872c0d7-0c33-4ec0-a65f-86ee460be775" -URL $SPSite.Url –Confirm:$false

            #reactivate
            Enable-SPFeature -identity "d872c0d7-0c33-4ec0-a65f-86ee460be775" -URL $SPSite.Url
        }
           

        $SPSite.Dispose()
    }
}


#foreach ($web in $site.AllWebs)

 Looking for OOTB feature IDs?

No Comments

There are no comments related to this article.