Objective
This purpose of this document is to get custom feature status (Activated/Deactivated) from selected sets of site collection configured in XML file using PowerShell script. The reusable script for this job has also attached with this document. This script offers,
  • To read a set of site collections, from which you want to get the feature status.
  • Generate a report in excel format which will provide all the site details and feature status info.
Business Case

S. No.Business Case
1For several reasons we SharePoint Developer/administrators want to check if the features are activated for site collections or not. Consider a case where after the deployment administrator want to check if this feature is been activated on site collection (say 10 site collection), manually going to every managefeatures.aspx page for site and web level is not feasible. Doing like this one by one is a painful job for administrator/developer. This script will allow administrator/developers to do the job just by configuring the XML file.

Targeted Audience
  • SharePoint Application Developers
  • SharePoint Administrator
  • SharePoint Architect
Offerings
  • One reusable PowerShell script is provided which needs to be run to get the feature status for selected set of site collections.
  • Format of XML file is provided where site collection needs to be configured.
Technical Details

Below are the technical details for this PowerShell script,

1: Pre-requisites
  1. Login to server with Farm administrator account and copy the folder and paste the same to the location where you want to keep it.
  2. Open the folder and Configure XML file as per your requirement.
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Configuration Environment="DEV" Version="1.0.0.0">
  3. <GlobalWebApplications>
  4. <GlobalWebApplication url="http://mywebapplicationURL">
  5. <CustomFeatures>
  6. <SiteScoped>
  7. <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">
  8. <Name>XYZ Site Columns Feature</Name>
  9. </Feature>
  10. <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">
  11. <Name>XYZ Content Types Feature</Name>
  12. </Feature>
  13. <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">
  14. <Name>XYZ Feature</Name>
  15. </Feature>
  16. </SiteScoped>
  17. <WebScoped>
  18. <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">
  19. <Name>XYZ Site Lists Feature</Name>
  20. </Feature>
  21. <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">
  22. <Name>XYZ Event Receivers Feature</Name>
  23. </Feature>
  24. </WebScoped>
  25. </CustomFeatures>
  26. </GlobalWebApplication>
  27. </GlobalWebApplications>
  28. </Configuration>
As shown in the below image; enter Web Application URL at https://myWebApplicationURL , configure a set of features at site as well as web level as shown in in above image.

Note: Name the file as Configuration.xml only.

2: Execution

Prerequisite:

Login to SharePoint Server as Farm Administrator and copy the required files (PowerShell script and configuration XML).Configure the XML file as per your requirement.

Run:
  1. Run the PowerShell Script as “Run as Administrator“.
  2. Browse the folder path where you have kept this PowerShell script file and execute a command as shown in below image.
Note:
  1. As shown in above image; Script will ask you for a path where you want to keep these backup file.

  2. In case you have typed site collection URL wrongly this script will show you an error in red color asking you to check if you have typed the URL correctly.

    error
3: Output Folder
  1. Please find below folder location which we have configured in our script to maintain a site collection backup output location.

  2. Output will be csv file with name as GetFeatureStatusResults, will contain information like,

    a. Site Template
    b. Site URL
    c. Site Creation Date
    d. Site collection level Feature status
    e. Web Level Feature status
PowerShell Script
  1. #check to see if the PowerShell Snapin is added
  2. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
  3. Add-PSSnapin Microsoft.SharePoint.PowerShell;
  4. }
  5. ## SharePoint DLL
  6. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
  7. $global:currentPhysicalPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
  8. [xml]$xmlinput = (Get-Content "$global:currentPhysicalPath\Configuration.xml")
  9. Function Get-SPWebApplication()
  10. {
  11. Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )
  12. return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
  13. }
  14. #EndRegion
  15. #Region Check Site Level Custom Features Status
  16. # ===================================================================================
  17. # Func: Check Site Level Custom Features status
  18. # Desc: Check custom features status across multiple site collections
  19. # ===================================================================================
  20. function Global_CheckSiteLevelFeatureStatus([System.Xml.XmlElement]$feature, [String]$url)
  21. {
  22. $identity = $feature.id
  23. $featureName = $feature.Name
  24. $featureObject = Get-SPFeature -Site $url | Where-Object { $_.id -eq $identity }
  25. if ($featureObject -eq $null)
  26. {
  27. return $url
  28. }
  29. }
  30. #Region Check Web Level Custom Features Status
  31. # ===================================================================================
  32. # Func: Check Web Level Custom Features status
  33. # Desc: Check custom features status across multiple site collections
  34. # ===================================================================================
  35. function Global_CheckWebLevelFeatureStatus([System.Xml.XmlElement]$feature, [String]$url)
  36. {
  37. $identity = $feature.id
  38. $featureName = $feature.Name
  39. $featureObject = Get-SPFeature -Web $url | Where-Object {$_.id -eq $identity}
  40. if ($featureObject -eq $null)
  41. {
  42. return $url
  43. }
  44. }
  45. #EndRegion
  46. #Region Check Custom Features Status
  47. # ===================================================================================
  48. # Func: Global_GetSiteCollectionFeatureStatus
  49. # Desc: Check custom features status across multiple site collections
  50. # ===================================================================================
  51. function Global_GetSiteCollectionFeatureStatus([xml]$xmlinput)
  52. {
  53. foreach($configWebApp in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication)
  54. {
  55. $webApp = Get-SPWebApplication($configWebApp.url)
  56. if($webApp -eq $null)
  57. {
  58. Write-host Web Application at url : $configWebApp.url does not Exists.. -foregroundcolor Red
  59. }
  60. else
  61. {
  62. Write-host Web Application at url : $configWebApp.url Exists.... -foregroundcolor Green
  63. $results = @()
  64. foreach($siteCollection in $webApp.Sites)
  65. {
  66. if($siteCollection -ne $null)
  67. {
  68. #Create an object to hold storage data
  69. $resultsData = New-Object PSObject
  70. $resultsData | Add-Member -type NoteProperty -name "Site URL" -value $siteCollection.Url -Force
  71. $resultsData | Add-Member -type NoteProperty -name "Site Template" -value $siteCollection.rootweb.WebTemplate -Force
  72. $resultsData | Add-Member -type NoteProperty -name "Site Created Date" -value $siteCollection.rootweb.Created.ToShortDateString() -Force
  73. ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeatures.SiteScoped.Feature)
  74. {
  75. $siteFeatureObject = Get-SPFeature -Site $siteCollection.Url | Where-Object { $_.id -eq $feature.id }
  76. if ($siteFeatureObject -eq $null)
  77. {
  78. $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "NO" -Force
  79. }
  80. else
  81. {
  82. $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "YES" -Force
  83. }
  84. }
  85. if($siteCollection.AllWebs.Count -gt 0)
  86. {
  87. ForEach($web in $siteCollection.AllWebs)
  88. {
  89. $resultsData | Add-Member -type NoteProperty -name "Site URL" -value $web.Url -Force
  90. ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeatures.WebScoped.Feature)
  91. {
  92. $webFeatureObject = Get-SPFeature -Web $web.Url | Where-Object {$_.id -eq $feature.id}
  93. if($webFeatureObject -eq $null)
  94. {
  95. $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "NO" -Force
  96. }
  97. else
  98. {
  99. $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "YES" -Force
  100. }
  101. }
  102. }
  103. $web.Dispose()
  104. }
  105. $results += $resultsData
  106. }
  107. else
  108. {
  109. Write-host -ForeGroundColor Red "Site Collection with this name does not exist. Please check if you have typed the URL correctly."
  110. }
  111. $siteCollection.Dispose()
  112. }
  113. $results | export-csv -Path $currentPhysicalPath/GetFeatureStatusResults.csv -notypeinformation -Force
  114. write-host "Report Exported successfully."
  115. }
  116. }
  117. }
  118. #EndRegion
  119. #Region function call
  120. Global_GetSiteCollectionFeatureStatus $xmlinput
  121. #EndRegion

Read more articles on PowerShell: