Powershell to determine Features activated / deactivated for all sites inside site collection

Recently, I’ve created power shell script to loop through all sites withing site collection and list all Features activated / deactivated.

Add-PsSnapin Microsoft.SharePoint.PowerShell

## SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$webApplicationURL = $args[0]
$outputFile = $args[1]

$webApp = Get-SPWebApplication $webApplicationURL
set-variable -option constant -name out -value $outputFile

if($webApp -ne $null)
{
 foreach($siteColl in $webApp.Sites)
 {
  if($siteColl -ne $null)
  {
   foreach($subWeb in $siteColl.AllWebs)
   {
    if($subWeb -ne $null)
    {
     # Print each Subsite
     "Site: " + $subWeb.Url | Out-File $outputFile -Append

     # Site Collection Features
     if($subWeb.Url -eq $webApplicationURL)
     {
      $siteFeatures = Get-SPFeature | Where-Object {$_.Scope -eq "Site" }
      if ($siteFeatures -ne $null)
      {
       foreach ($feature in $siteFeatures)
       {
	    # Actived feature
        if ((Get-SPFeature -Site $subWeb.Url | Where-Object {$_.Id -eq $feature.id}) -ne $null)
        {
         "SiteCollFeatureId: " + $feature.Id + "; SiteCollFeatureName: " + $feature.DisplayName + " activated" | Out-File $outputFile -Append
        }
	    # DeActived feature
        elseif ((Get-SPFeature -Site $subWeb.Url | Where-Object {$_.Id -eq $feature.id}) -eq $null)
        {
         "SiteCollFeatureId: " + $feature.Id + "; SiteCollFeatureName: " + $feature.DisplayName + " deactivated" | Out-File $outputFile -Append
        }
       }
      }
     }

	 # Site Features
     $siteFeatures = Get-SPFeature | Where-Object {$_.Scope -eq "Web" }
     if ($siteFeatures -ne $null)
     {
      foreach ($feature in $siteFeatures)
      {
	   # Actived feature
       if ((Get-SPFeature -Web $subWeb.Url | Where-Object {$_.Id -eq $feature.id}) -ne $null)
       {
        "SiteFeatureId: " + $feature.Id + "; SiteFeatureName: " + $feature.DisplayName + " activated" | Out-File $outputFile -Append
       }
	   # DeActived feature
       elseif ((Get-SPFeature -Web $subWeb.Url | Where-Object {$_.Id -eq $feature.id}) -eq $null)
       {
        "SiteFeatureId: " + $feature.Id + "; SiteFeatureName: " + $feature.DisplayName + " deactivated" | Out-File $outputFile -Append
       }
      }
     }
    }
    else
    {
     #Echo $subWeb "does not exist"
     $subWeb + "does not exist" | Out-File $outputFile -Append
    }
   }
   $siteColl.Dispose()
  }
  else
  {
   #Echo $siteColl "does not exist"
   $siteColl + " does not exist" | Out-File $outputFile -Append
  }
 }
}
else
{
 #Echo $webApplicationURL "does not exist, check the WebApplication name"
 $webApplicationURL + " does not exist" | Out-File $outputFile -Append
}

Remove-PsSnapin Microsoft.SharePoint.PowerShell

#Echo Finish
$webApplicationURL + " finish" | Out-File $outputFile -Append
"" | Out-File $outputFile -Append

Save the above power shell script as GetFeaturesInsideSiteCollection.ps1
To run the power shell script, you need to pass the site collection URL and output file path.

.\GetFeaturesInsideSiteCollection.ps1 http://yoursiteurl.com D:\temp\output.txt
Advertisements

Tagged: , , , ,

2 thoughts on “Powershell to determine Features activated / deactivated for all sites inside site collection

  1. Matthias Loring 05/17/2016 at 8:57 AM Reply

    Hi there! Beatiful Script. Is there any Chance to list all Custom Features too?

    Greetz
    Matthias

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: