SharePoint Web Part Usage Report – Find All Locations where a Particular web part is being Used

During Migration, I had to get the URLs where a particular web part is being used. So, Used this PowerShell script to generate the SharePoint web part usage report.

This script made compatible with SharePoint 2007, So it can be used in SharePoint 2010 and in SharePoint 2013 as well. Don’t forget to change the Web Application URL from “https://sharepoint.crescent.com” to yours.

SharePoint Web Part Usage Report using PowerShell:

Let’s find all instances of the “Excel Services” web part in our SharePoint environment. You may have to change the “Where” condition in Line#50 based on your requirements.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 
Function global:Get-SPWebApplication($WebAppURL)
{
 return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
}

#Get-SPSite cmdlet for MOSS 2007
function global:Get-SPSite($url)
 {
    return new-Object Microsoft.SharePoint.SPSite($url)
 }

Function global:Get-SPWeb($url)
{
  $site= New-Object Microsoft.SharePoint.SPSite($url)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();       
            }
    return $web
}
 
#Write Header to CSV File
"Page URL,  Web Part Name" | out-file "D:\Data\ExcelServicesWPs.csv"
 
#Get all Webs
$WebApp = Get-SPWebApplication "https://sharepoint.crescent.com" 
ForEach($site in $WebApp.Sites)
{
   #Iterate through webs
   Foreach ($web in $site.AllWebs)
   {
      #Get All Pages from site's Root into $AllPages Array
      $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
 
      #Search All Folders for Pages
      ForEach ($folder in $web.Folders)
      {
         #Add the pages to $AllPages Array
         $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
      }

      #Iterate through all pages
      ForEach($Page in $AllPages)
      {
          $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
    
          # Array to Hold Web Parts
          ForEach ($webPart in $webPartManager.WebParts | Where-Object {$_ -like "*Excel*"})
          {
             $Result = "$($web.site.Url)$($Page.ServerRelativeUrl), $($webpart.Title)"
             Write-Host "Web Part Found at: "$result
             $result | Out-File "D:\Data\ExcelServicesWPs.csv" -Append
          } 
      }
   }
}

The above script scans all libraries (not just “Site Pages” or “Pages” library! What if a web part page stored in a “documents” library?) from all sites where a particular webpart is being used and generates a report in CSV format.

But if you want to scan only publishing pages, Here is how the script goes:

$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$PagesLib = $PublishingWeb.GetPublishingPages()
ForEach($Page in $PagesLib)
{
          #Insert the code to scan web parts
}

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Web Part Usage Report – Find All Locations where a Particular web part is being Used

  • Hi. Thank you so much for this script. I am getting results, but am also getting an error. Since I’m getting results from multiple site collections on my farm, I don’t think the error is impeding anything, but I wanted to see what you think. Thanks!

    Exception calling “GetLimitedWebPartManager” with “2” argument(s): “Invalid URL argument.”
    At line:50 char:11
    $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUr …
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    FullyQualifiedErrorId : SPException

    Reply
  • Brother, I have run most of your scripts, but why does it return empty output file? except the hardcoded text
    What changes i have to apply in the script to get the correct output ??

    Reply
    • This script checks for “Excel Services” Web parts – You may change the “where” condition in Line#50 accordingly!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *