Listing all web parts in a site collection with PowerShell

As our current project has progressed we’ve quite naturally ended up with many pages of content. This is good news and highlights the success of the project – however in a solution with development on-going there are times when it is difficult to stay on top of all the content.

In particular if we’re interested in how a change to an existing piece of custom development may affect the users – or perhaps identifying where a configuration change may be required – it is a daunting prospect to have to go through the site by hand. Step forward PowerShell!

The below script loops through all pages in a site collection that are in either a publishing site (i.e. in a ‘pages’ library) or in a non-publishing site (i.e. in a ‘site pages’ library). [It currently doesn’t check the root of sites for pages (e.g. default.aspx) but could be easily modified to do so.] When it finds a page, the script then finds all web parts on a page. After looping through all the pages in all the sites of a site collection the results are then displayed in a grid view.

List All Web Parts - Grid View

This can then be copied and pasted into Excel and manipulated as required. (Note: I’ve deliberately made the URL columns impossible to read here!)

To use the script, either copy the contents into the Windows PowerShell Integrated Script Editor (ISE) and run from there, or save locally as a .ps1 and run directly from PowerShell. Don’t forget to update the URL at the bottom of the script!


# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue 

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
            Write-Host "Processing Web:" $pWeb.Url "..." -ForegroundColor Magenta
            foreach ($item in $pages.Items) {
                $fileUrl = $webUrl + "/" + $item.File.Url
                Write-Host "   " $fileUrl -ForegroundColor Green      
                $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 "   Not a publishing web:" $web.Url". Looking for Site Pages library." -ForegroundColor Magenta
            $pages = $null
            $pages = $web.Lists["Site Pages"]
            if ($pages) {
                Write-Host "   " $pages.Title "found." -ForegroundColor Green
                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 "    Site Pages library not found." -ForegroundColor Red                
        Write-Host "... completed processing" $web "..." -ForegroundColor Magenta    

$row = enumerateWebParts('http://mysitecollection')
$row | Out-GridView
share and enjoy
  • Print
  • Twitter
  • Digg
  • StumbleUpon
  • Yahoo! Buzz
  • Google Bookmarks
  • Facebook

9 comments to Listing all web parts in a site collection with PowerShell

  • Harry

    Thanks for the post.
    What would I add to show web parts from pages from the root (/default.aspx)?
    Or any web part page (not publishing page) for that matter..
    Thanks in advance.

    • Hi Harry – I think you’d need to add something into the else statement where it currently says “Site Pages library not found”. You could perhaps try putting into there something to populate the $item variable with the default.aspx page. I don’t have an environment in front of me right now to try this but maybe something along the lines of $web.GetFile(“default.aspx”) might work? After that, getting hold of the web parts should be the same as in the other areas of the script.


  • Any way to get the XSL from the content editor web parts? (not just the title)

  • I also used your script to get the Page Content information, summary links, page image, etc. Information. I added the following lines to the foreeach ($item in $pages.items) loop
    $output = $
    $output | fl
    (had to assign the properties to a var, then format that var to list format to get the full Name and Value of each page property on a publishing site.

    Once I can get the XSL, I can use this to document all of the content in the site, just from a script. (Then manually backup the Masterpage, layouts, css, images and any other global assets.)

    • Hi Eric,

      Thanks for the addition lines. If you’re still looking for a way to get the XSL then you can treat this just as any other property on the web parts, e.g. DisplayTitle. What I’ve done in the past is to manually get an instance of a web part on a test page through PowerShell and simply inspect it (using the gm command) – this then tells me what the properties are and how to reference them.

      Note – I’m not sure there is any XSL on a content editor web part though!

  • Patrick Bretelson


    This scripts works great. What would it take to get this to work in SharePoint 2007?

  • This script is ace! cheers

  • How do I alter the script to get the information from non-publishing sites?

  • […] googling for some minutes I fount a blog post of Glyn Clough (this is the url that inspired me (thank you Glyn :)) to create a new script that I just published on TechNet […]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>