Wednesday 13 August 2014

How to: Activate a feature on multiple site collections in the entire webapplication using PowerShell

A lot of times we have a need to activate a feature on multiple site collections in the webapplication.  This could be a custom feature to do branding   PowerShell makes tasks like these easy.  We’ll take what we learned from my original Activating Features with PowerShell post and use some common techniques to iterate through the all site collections in the webapplication and activate the desired feature.
Points to be noted:
1)$contentwebapp ="your webapplication url wher you have to activate site collection feature"
2)$fixupfeatureid= "your feature ID"
Please change the above mention parametes in the script below

$snapin = Get-PSSnapin | Where-Object { $_.Name -eq "Microsoft.SharePoint.Powershell" }
if ($snapin -eq $null) {
    Write-Host "[Your Company Name] Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
$fixupfeatureid=new-object system.guid("your Feature ID")
$contentwebapp = "Web Application URL"
get-spsite -webapplication $contentwebapp -limit all | %{
    $s=$_
            # fixup
            $removeErr=$null
            $s.Features|?{$_.DefinitionID -eq $fixupfeatureid}|%{
             Write-Host -ForegroundColor Yellow "Deactivating Fixup feature at " + $s.Url
                $s.Features.Remove($fixupfeatureid)|%{} }
            if($?){$removeErr=$error[0]}
            $addErr=$null
            Write-Host -ForegroundColor Green  "Activating Fixup feature at " + $s.Url
            $s.Features.Add($fixupfeatureid)|%{}
            if($?){$addErr=$error[0]}
            #new-object psobject -property @{url=$url;id=$_.DefinitionId; RemoveErr=$removeErr; AddErr=$addErr}
    $s.dispose()
}
This is a simple script that makes it easy to activate and deactivate features in bulk in entire webapplication.
Hope this will help you Guys .......

No comments:

Post a Comment