Find all pages using a specific Web Part

Scenario:
You are looking for all pages where a specific Web Part is used

PowerShell Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.WebPartPages")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

$wpName = "Register css web part";
$siteUrl = "http://site:port"
$libraryName = @("Pages");
$site = Get-SPSite $siteUrl;

foreach($spweb in $site.AllWebs)
{
 $url = $spweb.Url;
 foreach($library in $libraryName) {
  $list = $spweb.Lists[$library]
  foreach($listItem in $list.Items) {
   $WebPageUrl = $listItem.Url
   if ($WebPageUrl) {
    if ($spwebpart.Title -and $listItem.File.CheckOutType -ne "None") {
     Write-Host "Skipping checked out page $url/$WebPageUrl" -ForegroundColor Cyan
    }
    $spWpManager = $spweb.GetLimitedWebPartManager($WebPageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);
    foreach($spWebPart in $spWpManager.Webparts) {
     if($wpName -eq $spWebPart.Title) {
      Write-Host "Found '$wpName' on $url/$WebPageUrl" -ForegroundColor Yellow
     }
    }
   }
  }
 }
 $spweb.Dispose();

 if($spWpManager -ne $null) {  
  $spWpManager.Dispose();
 }  
}

Leave a Reply