How to Activate a Feature using PowerShell in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
December 7, 2014
 
Rate this article
 
Views
14979

In this article, let us see, how to Activate the Site Scoped, WebApplication Scoped Features using PowerShell in SharePoint 2013.

 $ErrorActionPreference = "Stop"
 cls
 
 ############################# Feature Names ###############################
 
 $SiteScopedFeature = "MyFeature.InternalName" #Target Site Collection
 
 $WebApplicationScopedFeature = "MyWebApplicationFeature.InternalName" #Target Web Application
 
 ######################################################################################
 
 $args = new-object string[] 1
 $args[0] = "CloudShare"
 
 function AddPowerShellSnapin()
 {
     try
     {
         Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green
         # Try to get the PowerShell Snappin.  If not, then adding the PowerShell snappin on the Catch Block
         Get-PSSnapin "Microsoft.SharePoint.PowerShell" 
     }
     catch
     {
         if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found"))
         {
             Add-PSSnapin "Microsoft.SharePoint.PowerShell"
         }
     }
     Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green
 }
 
 function ActivateFeature($DisplayName, $siteurl)
 {
     Write-Host "Activating " $DisplayName "In Site Collection " $siteurl
     $TempCount = (Get-SPSite  $siteurl | %{ Get-SPFeature -Site $_ } | Where-Object {$_.DisplayName -eq $DisplayName} ).Count
     if($TempCount -eq 0)
     {
         # if not, Enable the Feature.
         Get-SPFeature  $DisplayName | Enable-SPFeature -Url  $siteurl 
     }            
     else
     {
         # If already Activated, then De-Activate and Activate Again.
         Disable-SPFeature $DisplayName -Url $siteurl  –Confirm:$false
         Get-SPFeature  $DisplayName | Enable-SPFeature -Url  $siteurl 
     }
 }
 
 function ActivateFeatureInWebApplicationScope($DisplayName, $siteurl)
 {
         
     Write-Host "Activating " $DisplayName "In Web Application " $siteurl
     $TempCount = (Get-SPWebApplication $siteurl | %{ Get-SPFeature -WebApplication $_ } | Where-Object {$_.DisplayName -eq $DisplayName} ).Count
     if($TempCount -eq 0)
     {
         # if not, Enable the Feature.
         Get-SPFeature  $DisplayName | Enable-SPFeature -Url $siteurl 
     }            
     else
     {
         # If already Activated, then De-Activate and Activate Again.
         Disable-SPFeature $DisplayName -Url $siteurl  –Confirm:$false
         Get-SPFeature  $DisplayName | Enable-SPFeature -Url  $siteurl 
     }
 }
 
 try
 {
    
     AddPowerShellSnapin
     
     #Read arguement and store environment URL
     if($args[0] -eq 'Cloud')
     {
         $SiteCollectionURL ="http://SathishServer:1001/sites/Test"
         $WebApplicationURL ="http://SathishServer:1001/"
 
     }
     elseif($args[0] -eq 'DEV')
     {
 	# Give the URLs appropriately         
     }
     elseif ($args[0] -eq 'INT') 
     {
          	# Give the URLs appropriately
     }
     elseif ($args[0] -eq 'QA') 
     {
          	# Give the URLs appropriately
     }
     elseif ($args[0] -eq 'PROD') 
     {
          	# Give the URLs appropriately
     }
     else 
     {
         Write-Host -f Yellow "Environment not specified - please pass appropriate environment to deploy (Possible values are DEV INT QA PROD)"
         Log-Message $logFilePath  "Environment not specified - please pass appropriate environment to deploy (Possible values are DEV INT QA PROD)"
         exit
     }
    
     ActivateFeature $SiteScopedFeature $SiteCollectionURL
 
     ActivateFeatureInWebApplicationScope $WebApplicationScopedFeature $WebApplicationURL
     
 
     Write-Host "Script Execution Completed Successfully" -ForegroundColor Green 
 }
 catch
 {
     Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red  
 }
 

The script will actually De-Activate and Activate the Feature specified on the top of the script. The script is mostly self explanatory. Hence, I am not explaining much about that.

Happy Coding.

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment