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 -
- Name of content type
- Web Application URL
To get started, replace Content Type name and Web Application URL below with the one to be searched for,
- $myContentType = "Report Builder Report"
- $myWebApp = "http:
- if ((Get -PSSnapin "Microsoft.SharePoint.PowerShell" - ErrorAction SilentlyContinue) -eq $null) {
- Add -PSSnapin "Microsoft.SharePoint.PowerShell"
- }
- @results = @()
- $sites = Get -SPSite –Limit All –WebApplication $myWebApp
- try
- {
- foreach($site in $sites)
- {
- Write-Host "Looking in site– " $site.Url
- foreach($web in $site.AllWebs)
- {
- Write-Host $web.Url
- foreach($list in $web.lists)
- {
- foreach($ctype in $list.ContentTypes | ? { $_.Name –eq $myContentType})
- {
- foreach($item in $list.Items | ? { $_.ContentType.Name –eq $myContentType })
- {
- $RowDetails = @ {
- "List" = $list.Title
- "ListURL" = $list.DefaultView.Url
- "ContentType" = $item.ContentType.Name
- "Site" = $site.Url
- "Web" = $web.Url
- }
- $results += New-Object PSObject –Property $RowDetails
- break
- }
- }
- }
- $web.Dispose()
- }
- $site.Dispose()
- }
- }
- catch
- {
- $e = $_.Exception
- $line = $_.InvocationInfo.ScriptLineNumber
- $msg = $e.Message
- Write-Host –ForegroundColor Red "Caught Exception: $e at $line"
- Write-Host $msg
- Write-Host "Something went wrong"
- }
- $results | Export - CSV– Path C: \ContentTypeUsageReport.csv– NoTypeInformation
- Write-Host " === === === === === === === Completed! === === === === === === === === == "
The output file will look somewhat like this (after some Excel styling, of course).