Showing posts with label Feature. Show all posts
Showing posts with label Feature. Show all posts

Monday, 25 December 2017

PowerShell: Activate feature on all site levels.


#
# Description:
# Activate the site feature on all sub-sites
# This feature creates custom list
#
# Created Date:
# v1.0 - 27-11-2017
#
# Author:
# Vikas Bansal
#
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#feature ID to create custom list
$FeatureID="3e0d7f50-a6b7-45a6-b5a1-77950ef4327f"
#get all webs (sub-sites) under site collection
$WebsColl = Get-SPSite https://sp2013.com | Get-SPWeb -Limit ALL
#get the Feature
$Feature = Get-SPFeature -Limit All | Where { $_.ID -eq $FeatureID }
#check if Feature exists
if($Feature -ne $null) {
Write-Host "feature exists" -ForegroundColor DarkBlue -BackgroundColor Yellow
#iterate through through each sub-site activate the feature
foreach($web in $WebsColl){
#check if the Feature is activated
if (Get-SPFeature -web $web.Url | Where {$_.ID -eq $FeatureId}) {
Write-Host "feature is already activated on " $web.Url -ForegroundColor DarkBlue -BackgroundColor Yellow
}
else {
Write-Host "feature not activated on " $web.Url -ForegroundColor Yellow -BackgroundColor Red
#Activate the Feature
Enable-SPFeature –identity $FeatureID -URL $web.URL -Confirm:$false
Write-Host "feature is activated now on " $web.Url -ForegroundColor DarkBlue -BackgroundColor White
}
}
}
else {
Write-Host "feature does not exists" -ForegroundColor Yellow -BackgroundColor Red
}