Get the List of Features which are Active and InActive – SharePoint PowerShell

Sathish Nadarajan
 
Solution Architect
June 1, 2016
 
Rate this article
[Total: 0    Average: 0/5]
 
Views
9482

Usually we would have used PowerShell script to get the Active Features alone. In a recent requirement, I have to take the active and InActive Features as well. That too, in all the scopes. i.e., WebApp, Site, Web etc., Hence, as usual coming with a Code Snippet.

Here, the only Logic is, we need to take all the features in an object. The second object will have only the activated features. Hence, the difference will give us the inactive features.

 function GetFeatures()
 {
     Write-Host "Entered GetFeatures Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Entered GetFeatures Method"
     $script:Progress = "3:Entered"
     
     # Get the WebApplication
     $WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
 
     # Assign the XML Output File Paths
     $Features_XML_Path = $scriptBase + "\Reports-$LogTime\Features.xml"
     
     # Create the XML File Tags
     $xmlWriter = New-Object System.XMl.XmlTextWriter($Features_XML_Path,$Null)
     $xmlWriter.Formatting = 'Indented'
     $xmlWriter.Indentation = 1
     $XmlWriter.IndentChar = "`t"
     $xmlWriter.WriteStartDocument()
     $xmlWriter.WriteComment('Features List On the WebApplication ' + $WebApplication.DisplayName)
     $xmlWriter.WriteStartElement('WebApplication')
     $xmlWriter.WriteEndElement()
     $xmlWriter.WriteEndDocument()
     $xmlWriter.Flush()
     $xmlWriter.Close()
      
     # Call the WebApplication Feature method
     GetWebApplicationFeature $WebApplication
     
     # Collect the Site Collections
     $SiteCollections =  $WebApplication | Get-SPSite -Limit All
     
     # write the output on XML File
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $Features_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
     $xmlDoc.Save($Features_XML_Path)
      
     # Iterate through all the SiteCollections
     foreach($SiteCollection in $SiteCollections)
     {
         $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         
         # Call the SiteCollection Feature Method
         GetSiteCollectionFeature $SiteCollection
         
         # Get the Webs
         $Webs = Get-SPWeb -site $SiteCollection -Limit All 
         
         # Iterate through the Webs
         foreach($Web in $Webs)
         {
             # Call the Web Scope Features Method
             GetWebFeature $Web $siteCollectionName.Title
         }
     }    
          
     Write-Host "Completed GetFeatures Method" -ForegroundColor Yellow 
     Add-Content "$ProgressFile" "Completed GetFeatures Method"
     $script:Progress = "3:Success"
 }
             ############### Get Web Application Scoped Features ###########
             
 function GetWebApplicationFeature($WebApplication)
 {
     # Get All the WebApplication Scoped Feature
     $Features = Get-SPFeature | Where-Object {$_.Scope -eq "WebApplication" } # Farm, WebApplication, Site and Web
 
     # Get the Features activated on the Given WebApplication
     $WebApplicationFeatures = Get-SPFeature -WebApplication $WebApplication
     
     # write the output on XML File
     
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $Features_XML_Path);
     $FeaturesNode = $xmlDoc.CreateElement("Features")
     $xmlDoc.SelectSingleNode("//WebApplication").AppendChild($FeaturesNode)
      
     # Iterate through all the Available Feature     
     foreach($Feature in $Features)
     {
         # To get the Feature display Name, we are using the Feature Definition
         $cc = [System.Globalization.CultureInfo]::CurrentCulture;
         $featureDefn = (Get-SPFarm).FeatureDefinitions[$Feature.Id]
         $featureTitle = $($featureDefn.GetTitle($cc));
         
         # write the output on XML File
         
         $FeatureNameNode = $FeaturesNode.AppendChild($xmlDoc.CreateElement("FeatureDisplayName"));
         $FeatureNameNode.SetAttribute("Name", $featureTitle)
                                                 
         $FeatureIDNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("FeatureID"));
         $FeatureIDTextNode = $FeatureIDNode.AppendChild($xmlDoc.CreateTextNode($Feature.Id));
         
         $FeatureScopeNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("FeatureScope"));
         $FeatureScopeTextNode = $FeatureScopeNode.AppendChild($xmlDoc.CreateTextNode($Feature.Scope));
         
         $FeatureIsHiddenNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsHidden"));
         $FeatureIsHiddenTextNode = $FeatureIsHiddenNode.AppendChild($xmlDoc.CreateTextNode($Feature.Hidden));
         
         # Check whether the Feature is Active or InActive
         if (($WebApplicationFeatures | Where-Object {$_.Id -eq $Feature.id}) -eq $null)
         {
             $FeatureIsActivedNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsActivated"));
             $FeatureIsActivedTextNode = $FeatureIsActivedNode.AppendChild($xmlDoc.CreateTextNode("In Active"));
             
         }
         if (($WebApplicationFeatures | Where-Object {$_.Id -eq $Feature.id}) -ne $null)
         {
             $FeatureIsActivedNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsActivated"));
             $FeatureIsActivedTextNode = $FeatureIsActivedNode.AppendChild($xmlDoc.CreateTextNode("Active"));
              
         }
     }
      
     # write the output on XML File
      
     $xmlDoc.Save($Features_XML_Path)
      
 }
 
         ####################### Get the Site Collection Scoped Feature #################################
 
 function GetSiteCollectionFeature($SiteCollection)
 {
     # Get the Site Collection Object
     $siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}} 
         
     # write the output on XML File
      
     # Create the Initial Solution Node
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $Features_XML_Path);
     $siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
     $xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
     $siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
     $xmlDoc.Save($Features_XML_Path)
     
     $featuresNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("Features"));
     $subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SubSites"));
     $xmlDoc.Save($Features_XML_Path)
     
     # Get all the Site Collection Scoped Features 
     $Features = Get-SPFeature | Where-Object {$_.Scope -eq "Site" } # Farm, WebApplication, Site and Web
     
     # Get the Features activated on the Given Site collection
     $siteCollectionFeatures = Get-SPFeature -Site $SiteCollection
     
     # Iterate through the Features
     foreach($Feature in $Features)
     {
         # To get the Feature display Name, we are using the Feature Definition
         $cc = [System.Globalization.CultureInfo]::CurrentCulture;
         $featureDefn = (Get-SPFarm).FeatureDefinitions[$Feature.Id]
         $featureTitle = $($featureDefn.GetTitle($cc));
         
         # Write Output on XML File 
         $FeatureNameNode = $FeaturesNode.AppendChild($xmlDoc.CreateElement("FeatureDisplayName"));
         $FeatureNameNode.SetAttribute("Name", $featureTitle)
                                                 
         $FeatureIDNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("FeatureID"));
         $FeatureIDTextNode = $FeatureIDNode.AppendChild($xmlDoc.CreateTextNode($Feature.Id));
         
         $FeatureScopeNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("FeatureScope"));
         $FeatureScopeTextNode = $FeatureScopeNode.AppendChild($xmlDoc.CreateTextNode($Feature.Scope));
         
         $FeatureIsHiddenNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsHidden"));
         $FeatureIsHiddenTextNode = $FeatureIsHiddenNode.AppendChild($xmlDoc.CreateTextNode($Feature.Hidden));
         
         # Check if the Feature is Active or In Active    
         if (($siteCollectionFeatures | Where-Object {$_.Id -eq $Feature.id}) -eq $null)
         {
             $FeatureIsActivedNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsActivated"));
             $FeatureIsActivedTextNode = $FeatureIsActivedNode.AppendChild($xmlDoc.CreateTextNode("In Active"));
         }
         if (($siteCollectionFeatures | Where-Object {$_.Id -eq $Feature.id}) -ne $null)
         {
             $FeatureIsActivedNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsActivated"));
             $FeatureIsActivedTextNode = $FeatureIsActivedNode.AppendChild($xmlDoc.CreateTextNode("Active"));
         }
     }
     
     # Save the XML File        
     $xmlDoc.Save($Features_XML_Path)
     
 }
 
             ######################## Get the Web Scoped Features ####################################
             
 function GetWebFeature($Web, $siteCollectionName)
 {
     # write the output on XML File
     $xmlDoc = [System.Xml.XmlDocument](Get-Content $Features_XML_Path);
     $ns = New-Object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
     $ns.AddNamespace("ns", $xmlDoc.DocumentElement.NamespaceURI)
     $filter = "//ns:SiteCollection[@Name='" + $siteCollectionName + "']"
     $siteCollectionNode = $xmlDoc.SelectSingleNode($filter, $ns)
     
     $subSitesNode = $siteCollectionNode.SelectSingleNode("SubSites")
     
     $subSiteNode = $xmlDoc.CreateElement("SubSite")
     $subSitesNode.AppendChild($subSiteNode)
     $subSiteNode.SetAttribute("Name", $Web.Title)
     $subSiteNode.SetAttribute("Url", $Web.Url)
     $subSiteNode.SetAttribute("WebID", $Web.Id)
     $parentWebTitle = ""
                                 
     if($Web.ParentWebID -ne "00000000-0000-0000-0000-000000000000")
     {
         $parentWeb = $SiteCollection.OpenWeb($Web.ParentWebID)
         $parentWebTitle = $parentWeb.Title
         
     }
     else
     {
         $parentWebTitle = "RootWeb"
     }    
     $subSiteNode.SetAttribute("ParentWebName", $parentWebTitle)
     $subSiteNode.SetAttribute("ParentWebID", $Web.ParentWebID)
                                 
     
     $FeaturesNode = $xmlDoc.CreateElement("Features")
     $subSiteNode.AppendChild($FeaturesNode)
     
     $xmlDoc.Save($Features_XML_Path)
      
     # Get all the Web Scoped Features
     $Features = Get-SPFeature | Where-Object {$_.Scope -eq "Web" } # Farm, WebApplication, Site and Web
     
     # Get the Web Scoped Features which are active
     $WebFeatures = Get-SPFeature -Web $Web
     
     # Iterate through the Features
     foreach($Feature in $Features)
     {
         $cc = [System.Globalization.CultureInfo]::CurrentCulture;
         $featureDefn = (Get-SPFarm).FeatureDefinitions[$Feature.Id]
         $featureTitle = $($featureDefn.GetTitle($cc));
         
         # write the output on XML File
         $FeatureNameNode = $FeaturesNode.AppendChild($xmlDoc.CreateElement("FeatureDisplayName"));
         $FeatureNameNode.SetAttribute("Name", $featureTitle)
                                                 
         $FeatureIDNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("FeatureID"));
         $FeatureIDTextNode = $FeatureIDNode.AppendChild($xmlDoc.CreateTextNode($Feature.Id));
         
         $FeatureScopeNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("FeatureScope"));
         $FeatureScopeTextNode = $FeatureScopeNode.AppendChild($xmlDoc.CreateTextNode($Feature.Scope));
         
         $FeatureIsHiddenNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsHidden"));
         $FeatureIsHiddenTextNode = $FeatureIsHiddenNode.AppendChild($xmlDoc.CreateTextNode($Feature.Hidden));
         
         # Check whether the Feature is Active or In Active
         if (($WebFeatures | Where-Object {$_.Id -eq $Feature.id}) -eq $null)
         {
             $FeatureIsActivedNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsActivated"));
             $FeatureIsActivedTextNode = $FeatureIsActivedNode.AppendChild($xmlDoc.CreateTextNode("In Active"));
         }
         if (($WebFeatures | Where-Object {$_.Id -eq $Feature.id}) -ne $null)
         {
             $FeatureIsActivedNode = $FeatureNameNode.AppendChild($xmlDoc.CreateElement("IsActivated"));
             $FeatureIsActivedTextNode = $FeatureIsActivedNode.AppendChild($xmlDoc.CreateTextNode("Active"));
         }
     }
     
     # Save the XML
     $xmlDoc.Save($Features_XML_Path)
 }
 

The snippet is self-explanatory. I know it is a lengthy snippet. But, wanted to share with the community.

Happy Coding,

Sathish Nadarajan.

Category : PowerShell, SharePoint

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
[Total: 0    Average: 0/5]
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment