<# Unpublish-ContentTypes.ps1 A script to unpublish orphaned content types from the termstore database.These are content types that were deleted from Content Type Hub without being unpublished first. Parameters: SubScriber: Provide the URL of a site collection that subscribes to the Content Type Hub. ContentTypeHub: Provide the URL of the Content Type Hub site collection Unpublish: Specify the switch if you want to actually unpublish content types Examples: To get a list of orphaned content types without unpublishing them: .\Unpublish-ContentTypes.ps1 -SubscriberSite https://site.example.com -ContentTypeHub http://site.example.com/sites/cth To get a list of orphaned content types and unpublish them .\Unpublish-ContentTypes.ps1 -SubscriberSite https://site.example.com -ContentTypeHub http://site.example.com/sites/cth -Unpublish #> Param( [Parameter(Mandatory=$True, Position=1)] [string]$SubscriberSite, [Parameter(Mandatory=$True, Position=2)] [string]$ContentTypeHub, [switch]$Unpublish ) # Load PowerShell Snap-in if needed if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) { Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" Add-PSSnapin Microsoft.SharePoint.PowerShell } $web = Get-SPWeb $SubscriberSite $ctsite = Get-SPSite $ContentTypeHub $ctPublisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($ctsite) foreach($ct in $web.ContentTypes) { if ( $ct.XmlDocuments -like "*SharedContentType*") { try { #if (-not $ctsite.RootWeb.ContentTypes[$ct.Name]) if (-not $ctsite.RootWeb.ContentTypes[$ct.ID]) { Write-Host "Found a published orphan content type:" -ForegroundColor DarkGray Write-Host "Name: $($ct.Name) `nID: ($($ct.ID))`n" -ForegroundColor DarkYellow if($unpublish) { $ctPublisher.UnPublish($ct) Write-Host "Content Type " -ForegroundColor Green -NoNewline Write-Host "$($ct.Name)" -ForegroundColor DarkYellow -NoNewline Write-Host " was unpublished successfully`n" -ForegroundColor Green } } } catch { Write-Host "Error unpublishing ContentType $($ct.Name) – ($($ct.ID)): $error[0]" -ForegroundColor Red } } } $web.Dispose() $ctsite.Dispose()