magritton
10/12/2016 - 10:47 PM

This snippet search an entire web application for a webpart's usage. If the wepart is used on a page then it's URL is written to a text file

This snippet search an entire web application for a webpart's usage. If the wepart is used on a page then it's URL is written to a text file.

$webApplicationName = "https://xxxx"
$path = "f:\temp\wpSearch.txt"
$wa = Get-SPWebApplication $webApplicationName

foreach($site in $wa.Sites)
{
    $site.Url
    foreach ($web in $site.AllWebs) 
    {
		$web.Url
		foreach($list in $web.Lists)
		{
				if($list.BaseType -eq "DocumentLibrary")
				{
					foreach ($item in $list.Items) {
						$file = $item.File
						if($file.name -like "*aspx"){
							$url = $file.ServerRelativeUrl
							$fileUrl = $file.ServerRelativeUrl
							$manager = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared)
							$wps = $manager.webparts
							$manager.Dispose()
							foreach($wp in $wps)
							{
									if($wp -like "*Slide*"){
									$url | out-file $path -append
									$wp.WebBrowsableObject.Title | out-file $path -append
								}#end if like name
							}#end for each web part in page
						}#end if file type
					}#end for each item
				}#end if document library
		}#end for each list
		foreach ($file in $web.Files) {
				if($file.name -like "*aspx")
				{
					$url = $file.ServerRelativeUrl
					$fileUrl = $file.ServerRelativeUrl
					$manager = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared)
					$wps = $manager.webparts
					$manager.Dispose()
					foreach($wp in $wps)
					{
						if($wp -like "*Slide*")
						{
							$url | out-file $path -append
							$wp.WebBrowsableObject.Title | out-file $path -append
						}#end if like name
					}#end for each web part in page
				}#end if file type
			}#end for each file
    }#end for each web
}#end for each site