SharePoint Pals
 | Sign In
Get the List of Features which are Active and InActive – SharePoint PowerShell
by Sathish Nadarajan 1 Jun 2016
Author
SharePoint MVP
Visits   
Today  :   1     Total  :    2979

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation