Wednesday, 13 August 2014

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

A lot of times we have a need to activate a feature on multiple webs in the entire 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 Webs 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)featureid= "your feature ID"
Please change the above mention parametes in the script below
copy the below mention script in notpad and save it with extension '.ps1'
run it on server.
$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"
}
Try
{
 $FeatureID ="Feature id"
 $contentwebapp ="Web Application URL"
 Write-Host -f Green "Found content web application at " + $contentwebapp.Url
 $webApp = Get-SPWebApplication $contentwebapp
 if($webApp -ne $null)
{
 get-spsite -webapplication $contentwebapp -limit all | %{
   $siteColl=$_
   
   foreach($subWeb in $siteColl.AllWebs)
   {
    if($subWeb -ne $null)
    {
     # Print each Subsite
     Write-Host -f Yellow "Found web at:" $subWeb.Url
     #Get Feature ID
   
      #Check whether Feature to be activated is already activated for this subsite
     $siteFeature = Get-SPFeature -Web $subWeb | Where {$_.ID -eq $FeatureID.Id}
    if ($siteFeature -ne $null)
     {

 Enable-SPFeature -Identity $FeatureID -Confirm:$false -Url $subWeb.url -Force
Write-Host -f yellow "feature is activated at:" $subWeb.Url
write-Host "[your company name]================================================================"
      }
      else
      {

       disable-SPFeature -Identity $FeatureID -Confirm:$false -Url $subWeb.url
  Write-Host -f green "feature is de-activated at:" $subWeb.url
       Enable-SPFeature -Identity $FeatureID -Confirm:$false -Url $subWeb.url
       Write-Host -f green "feature is activated at:" $subWeb.url
  write-Host "[your company name]================================================================"
      }

   
     $subWeb.Dispose()

    }
   }
   $siteColl.Dispose()

 

 }

}

}

catch

{
   Write-Host -Object "Error occurred while running this script "

}
This is a simple script that makes it easy to activate and deactivate web level features in bulk in entire webapplication.
Hope this will help you Guys .......

No comments:

Post a Comment