Activate sharepoint features, find if not already activated

16/03/2013 00:45

#Input parameters

$webUrl = "https://server/sitecollection/subsite"

$SiteUrl= "https://server/sitecollection"

$FeatureName = "MyFeatureName"

$webAppUrl = (Get-SPSite $SiteUrl).WebApplication.Url.TrimEnd('/');

$IsSandbox = $false #or $true if feature is from sandbox solution

 

#Activate Features (common for farm and sandbox solution - if sandbox, use feature Id instead of name)

#Activate Web Feature

Enable-SPFeature -Identity $FeatureName -URL $webUrl -force

 #Dectivate Web Feature

 Disable-SPFeature -Identity $FeatureGuid -Url $webUrl -Force -Confirm:$false

 Enable-SPFeature -Identity $FeatureGuid -URL $webUrl -force

 

 

#Activate Site Feature

Enable-SPFeature -Identity $FeatureName -URL $siteUrl

 

#Activate Web Application Feature

Enable-SPFeature -Identity $FeatureName -URL $webAppUrl

 

#Activate Farm Feature

Enable-SPFeature -Identity $FeatureName

 

#Find out if feature is not already active

#Web

if($IsSandBox){

    $feature = Get-SPFeature –Site $SiteUrl -Sandboxed | Where-Object {$_.DisplayName -eq $FeatureName}

}

else{

    $feature = Get-SPFeature –Web $webUrl | Where-Object {$_.DisplayName -eq $FeatureName}

}

$Features = (Get-SPWeb $webUrl).Features

$activeFeature = $Features | Where-Object {($_.DefinitionId -eq $feature.Id)}

if ($activeFeature -ne $null)

{

    Write-Host "Feature $FeatureName is already activated."

}

 

#Site

if($IsSandBox){

    $feature = Get-SPFeature –Site $siteUrl -Sandboxed | Where-Object {$_.DisplayName -eq $FeatureName}

}

else{

    $feature = Get-SPFeature –Site $siteUrl | Where-Object {$_.DisplayName -eq $FeatureName}

}

$Features = (Get-SPSite $siteUrl).Features

$activeFeature = $Features | Where-Object {($_.DefinitionId -eq $feature.Id)}

if ($activeFeature -ne $null)

{

    Write-Host "Feature $FeatureName is already activated."

}

 

#WebApplication (no web application features in sandbox solution)

$feature = Get-SPFeature –WebApplication $webAppUrl | Where-Object {$_.DisplayName -eq $FeatureName}

$Features = (Get-SPWebApplication $webAppUrl).Features

$activeFeature = $Features | Where-Object {($_.DefinitionId -eq $feature.Id)}

if ($activeFeature -ne $null)

{

    Write-Host "Feature $FeatureName is already activated."

}

 

#Farm (no farm features in sandbox solution)

$feature = Get-SPFeature | Where-Object {$_.DisplayName -eq $FeatureName}

$activeFeature = Get-SPFeature -Farm | Where-Object {$_.DisplayName -eq $FeatureName}

if ($activeFeature -ne $null)

{

    Write-Host "Feature $FeatureName is already activated."

}