Listing web parts used on all site pages using Powershell


UPDATED 2012-03-17: Added a link to the Powershell script file because the coded looked pretty bad on the post. lol

Powershell is pretty much the 8th wonder of the world, or at least, of my world. Almost everyday I learn a new trick I can do using Powershell.

A common problem for several SharePoint Administrators is to understand which web parts are used on their sites and how much they are used. The following script takes care of it going to all sites (SPWebs) on a web application and saving  to a text file all web parts found on existing pages.

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")

function ListWebParts($web) {
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web))
{
$webPublish = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $webPublish.GetPublishingPages()

foreach($page in $pages)
{
$manager = $web.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webCollection = $manager.WebParts
if($webCollection.Count -ne 0)
{
for($i =0;$i -lt $webCollection.Count; $i++)
{
$sw.writeline($web.url + "/" + $page.url + ";" + $webCollection[$i].GetType().Name + ";" +    $webCollection[$i].Title)
}
}
}
}
}

function LoadSPSite()
{
$siteURL = "http://YOUR SITE'S URL"
$site = New-Object Microsoft.SharePoint.SPSite($siteURL)

foreach($web in $site.allwebs)
{
write-output $web.Title
ListWebParts($web)
}
$site.Dispose()
}

$sw = [System.IO.StreamWriter] "PATH TO LOG FILE"
$sw.writeline("Page URL;Web Part Type;Web Part Title")
LoadSPSite
$sw.close()

 

See you,

Amadeu.

Advertisements

One Response to Listing web parts used on all site pages using Powershell

  1. David Remillard says:

    Thanks! Very useful script!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: