In SharePoint online, how do we remove Content Type from the Library by using Powershell ? I have tried using the CSOM code shown below but it doesn't seem to work:
$ctx.credentials = $creds
$contentTypes = $ctx.web.contenttypes
$ctx.load($contentTypes)
$ctx.executeQuery()
foreach($ct in $contentTypes){
write-host $ct.name
}
$ctName = "TCLDocument"
#Load the content types collection.
$ctx.Load($ctx.Web.Lists)
$ctx.ExecuteQuery()
foreach( $ll in $ctx.Web.Lists)
{
if($ll.Title -eq $ctName){
$ctx.Load($ll.ContentTypes)
try
{
$ctx.ExecuteQuery()
}
catch
{
}
Write-Host $ll.Title -ForegroundColor Green
foreach($cc in $ll.ContentTypes)
{
Write-Output $cc.Name
if($cc.Name = "Document"){
$ctExists = $true
$existingCT = $ct
Write-Host "Content Type $ctId 1 found" -ForegroundColor Red
break
}
}
if($ctExists){
$existingCT.DeleteObject()
$ctx.ExecuteQuery()
Write-Host "Content Type $ctId 2 found" -ForegroundColor Red
break
}
}