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.
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!
cls # 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