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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

One thought on “SharePoint Online: Remove a Content Type from All Lists in a Site Collection using PowerShell

  • Hi – is there a version of this script which uses PnPOnline to cope with MFA?

    I have an ongoing support ticket with MS and need to provide them a list of all our SharePoint sites that have a troublesome undelete-able content type. I’m looking for something to help me export a list of sites and the libraries within those sites that have this content type installed.

    Thanks for reading to the end 🙂

    Reply

Leave a Reply

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