SharePoint Online: Remove a Content Type from All Lists in a Site Collection using PowerShell

Requirement: Remove Content Type from All Lists and Libraries in a SharePoint Online Site Collection.

PowerShell to Remove Content Type from All Lists and Libraries in a Site Collection

In SharePoint Online, there may be times when you want to remove a content type from all the lists in a site collection. For example, maybe you have created a content type and realized later that it was not the best choice, or you want to stop users from adding items of that type to lists on the site. This can be done manually through the SharePoint user interface, but it can be time-consuming. In this post, I will show you how to remove a content type from all lists in a site collection using PowerShell.

This PowerShell script iterates through all lists and libraries of a given site collection, scans for the provided content type and removes it if any underlying items are not using it!

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Function to remove content type from a site collection
Function Remove-SPOContentType([String]$SiteURL, [String]$ContentTypeName)
{
    Try{
        Write-host -f Yellow "Processing Site:" $SiteURL
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get All Lists of the Web
        $Ctx.Load($Ctx.Web)
        $Ctx.Load($Ctx.Web.Lists)
        $Ctx.Load($ctx.Web.Webs)
        $Ctx.ExecuteQuery()
 
        #Get content types of each list from the web
        ForEach($List in $Ctx.Web.Lists)
        {
            $ContentTypes = $List.ContentTypes
            $Ctx.Load($ContentTypes)
            $Ctx.ExecuteQuery()             

            #Get each content type data
            ForEach($CType in $ContentTypes)
            {
                If($CType.Name -eq $ContentTypeName)
                {
                    Try{
                        #Remove Content Type from List
                        $CType.DeleteObject()
                        $Ctx.ExecuteQuery()
                        Write-host -f Green "Removed Content Type from List/Library:" $List.Title
                        }
                    Catch {
                    write-host -f Red "`tError Removing Content Type from '$($List.Title)' :" $_.Exception.Message
                    }
                }
            }
        }
 
         #Iterate through each subsite of the current web and call the function recursively
        foreach ($Subweb in $Ctx.web.Webs)
        {
            #Call the function recursively to process all subsites underneaththe current web
            Remove-SPOContentType $Subweb.url $ContentTypeName
        }
    }
    Catch {
    write-host -f Red "Error Removing Content Type:" $_.Exception.Message
    }
}
 
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Project Proposal"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Call the function to get the content type usage
Remove-SPOContentType -SiteURL $SiteURL -ContentTypeName $ContentTypeName

This method is faster and more efficient than manually removing content types from lists through the user interface. By using PowerShell, you can automate the process and ensure that content types are removed consistently across all lists in a site collection.

Related posts:

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published. Required fields are marked *