Find What SharePoint Sites Use a Specific WebPart using PowerShell

I know there are tons of people out there having the same issue. How do you figure out where in a SharePoint environment is a certain web part used. The following PowerShell script prompts you for the Full name of the web part’s type, as well as for the url of the web application to look into. It will scan all publishing and system pages and return a list of all pages where that specific web part is being used! Enjoy.​

function FindWebPart($webPartName) 
{
$webAppUrl = Read-Host “URL of the Web Application”
$webApplication = Get-SPWebApplication $webAppUrl
foreach($site in $webApplication.Sites)
{  
foreach($web in $site.AllWebs) 
{
        if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) 
{
            $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
            $pages = $pWeb.PagesList
            foreach ($item in $pages.Items) 
{
                $fileUrl = $webUrl + “/” + $item.File.Url
                $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                $wps = $manager.webparts
foreach($webpart in $wps)
{
if($webPartName -eq $webpart.GetType().ToString())
{
Write-Host “Found” $webpart.GetType().ToString() “at” $web.Url -BackgroundColor Green -ForegroundColor Black
}
}
}
        }
        else
{
            $pages = $null
            $pages = $web.Lists[“Site Pages”]            
if ($pages) 
{                
foreach ($item in $pages.Items) 
{
                    $fileUrl = $webUrl + “/” + $item.File.Url
                    $manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
                    $wps = $manager.webparts
foreach($webpart in $wps)
{
if($webPartName -eq $webpart.GetType().ToString())
{
Write-Host “Found” $webpart.GetType().ToString() “at” $web.Url -BackgroundColor Green -ForegroundColor Black
}
}
                    }
            }
        }  
$web.Dispose()  
    }
$site.Dispose()
}
}
$webPartName = Read-Host “Name of the webpart”
$row = FindWebPart($webPartName)

Microsoft Premier Field Engineer - SharePoint

Leave a Comment

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

*
*