This script will give you the usage details and dependencies of any content type, such as - list URL, Web URL, etc. and wherever that is being used. This might come in handy when you wish to delete the content types and you have to make sure of all its dependencies and references getting cleaned.

The output will be in the form of a CSV file where the above-mentioned content type is used. Input parameters used are -

  1. Name of content type
  2. Web Application URL

To get started, replace Content Type name and Web Application URL below with the one to be searched for,

  1. $myContentType = "Report Builder Report"
  2. $myWebApp = "http://myWebAppURL.com"
  3. if ((Get -PSSnapin "Microsoft.SharePoint.PowerShell" - ErrorAction SilentlyContinue) -eq $null) {
  4. Add -PSSnapin "Microsoft.SharePoint.PowerShell"
  5. }
  6. @results = @()
  7. $sites = Get -SPSite –Limit All –WebApplication $myWebApp
  8. try
  9. {
  10. foreach($site in $sites)
  11. {
  12. Write-Host "Looking in site– " $site.Url
  13. foreach($web in $site.AllWebs)
  14. {
  15. Write-Host $web.Url
  16. foreach($list in $web.lists)
  17. {
  18. foreach($ctype in $list.ContentTypes | ? { $_.Name –eq $myContentType})
  19. {
  20. foreach($item in $list.Items | ? { $_.ContentType.Name –eq $myContentType })
  21. {
  22. $RowDetails = @ {
  23. "List" = $list.Title
  24. "ListURL" = $list.DefaultView.Url
  25. "ContentType" = $item.ContentType.Name
  26. "Site" = $site.Url
  27. "Web" = $web.Url
  28. }
  29. $results += New-Object PSObject –Property $RowDetails
  30. break
  31. }
  32. }
  33. }
  34. $web.Dispose()
  35. }
  36. $site.Dispose()
  37. }
  38. }

  39. catch
  40. {
  41. $e = $_.Exception
  42. $line = $_.InvocationInfo.ScriptLineNumber
  43. $msg = $e.Message
  44. Write-Host –ForegroundColor Red "Caught Exception: $e at $line"
  45. Write-Host $msg
  46. Write-Host "Something went wrong"
  47. }

  48. $results | Export - CSV– Path C: \ContentTypeUsageReport.csv– NoTypeInformation
  49. Write-Host " === === === === === === === Completed! === === === === === === === === == "

The output file will look somewhat like this (after some Excel styling, of course).

output