List All Web Parts in a SharePoint 2010 Site Collection Using Windows PowerShell

I won’t make this overly complex, but if your organization is rapidly growing with SharePoint and you, farm admin, or your site collection administrators, or even your site owners are all super excited about SharePoint’s capabilities and you are provisioning sites or pages to justify the ever growing demands of your business, then you probably are adding hundreds of web parts to your sites and pages.

In most cases, these web parts are simply what shipped with the product and what gets placed on the sites once you provision them such as ‘Shared Documents’ or ‘Announcements’. In other cases, they maybe web parts developed in house by your development team to meet a specific demand such as ‘a moving average chard web part’ or even purchased from a third party vendor like Quest Software or Bamboo Solutions.

In any case, as your SharePoint succeeds and grows, and again, as you drop more web parts to your pages, the need to control and understand were these web parts are becomes super important. You will want to know which web parts are open and which ones are closed, which ones are used and which ones are not, which ones are affecting load time on pages and which ones are causing errors.

The video below captures the steps involved in executing a Windows PowerShell script that will loop through pages on publishing and non-publishing sites, finds all web parts, and outputs the result to a grid view.

Video length: 2:27

Use the below script to list all web parts in a site collection. The script loops through all pages, publishing and non-publishing sites, finds all web parts, and displays them on a grid view.

Note: Make sure you change the URL at the bottom of the script to your site collection URL.

 function enumerateWebParts($Url) {
    $site = new-object Microsoft.SharePoint.SPSite $Url    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
                $wps | select-object @{Expression={$pWeb.Url};Label=”Web URL”},@{Expression={$fileUrl};Label=”Page URL”}, DisplayTitle, IsVisible, @{Expression={$_.GetType().ToString()};Label=”Type”}
            }
        }
        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
                    $wps | select-object @{Expression={$pWeb.Url};Label=”Web URL”},@{Expression={$fileUrl};Label=”Page URL”}, DisplayTitle, IsVisible, @{Expression={$_.GetType().ToString()};Label=”Type”}
                }
            }
            else {
            }
        }        Write-Host “… completed processing” $web
    }
}$row = enumerateWebParts(‘http://sharepoint2010/sites/spquester’)
$row | Out-GridView

If the Integrated Script Editor (ISE) is not installed, execute the following script then go back to the script above and execute it.

Import-Module ServerManagerAdd-WindowsFeature PowerShell-ISE

Leave a Comment