I have the following script that will loop through each web/list/content type looking for a specific content type. If it finds it, and that content type is not in use, then it will delete it and update() the list, otherwise the error will be caught.
Seems to work fine except I have somehow created an infinite loop and the same lists will be outputted to the console over and over until I break out of the script. Can someone see where in my code this maybe happening?
foreach ($web in $site.AllWebs) { foreach ($lst in $web.lists | ?{$_.Fields.ContainsField($field)}) { foreach ($ctype in $lst.ContentTypes) { if ($ctype.Name -eq $ctName) { try { $ctToRemove = $ctype $location = $lst.DefaultViewUrl Write-Host "attempting to delete content type: $ctName at: $location" -ForegroundColor Yellow $lst.ContentTypes.Delete($ctToRemove.Id) $lst.Update() Write-Host "$ctype.Name deleted" write-Host "--------------------------------------------" } catch { write-host "Unable to delete the content type at $location, it is probably in use" -ForegroundColor Yellow write-Host "--------------------------------------------" } } } } } $web.Dispose()