PowerShell Snippet to Check Whether WebApplication Level Feature is Activated or not if not then Activate the Feature

  1. <#  
  2. .SYNOPSIS  
  3. Activating WebApplication level features if not already activated  
  4.    
  5. .PARAMETER Url  
  6. The URL of the web application  
  7.    
  8. .PARAMETER featureID  
  9. ID of feature which need to be activated  
  10. #>  
  11.    
  12. param  
  13. (  
  14. [parameter(Mandatory=$true)][string]$Url,  
  15. [parameter(Mandatory=$true)][string]$featureID  
  16. )  
  17.    
  18. #Activate web application Feature  
  19. #----------------------------------------------------  
  20. $webApplication = Get-SPWebApplication -Identity $Url  
  21.    
  22. #Check whether Feature to be activated is already activated  
  23. $feature = Get-SPFeature -WebApplication $webApplication | where {$_.Id -eq $fatureID}  
  24.    
  25. if ($feature)  
  26. {  
  27.       Write-Host "feature with ID" $fatureID "is already activated"  
  28. }  
  29. else  
  30. {  
  31.       Enable-SPFeature -Identity $fatureID -Url $webApplication.url  
  32.       Write-Host "feature with ID" $fatureID "has been activated at :" $webApplication.url  
  33. }