Add-Remove Content Types to SharePoint List or Library using PowerShell

SharePoint content types enable users to organize related data together. Everything is built around the content types in SharePoint to provide consistency and re-usability. For instance, All list templates such as Announcements and Tasks are built with content types “Announcement” and “Task”, respectively! Typically, a content type consists of template and metadata columns.  E.g., let’s consider the “Sales Proposal document” content type, which may be created at the site collection with a template for sales proposal and columns to capture its related metadata, and can be associated with any number of document libraries.

By default, content types are scoped at the site collection level. When you add a content type to a SharePoint list, it’s copied from the site content type gallery to the list, which means when you customize a content type at the list level, it doesn’t affect the parent content type, which lives at the site collection level.

In OOPS terminology, you can consider “Content Type” as the class and items created from that content type as “Objects”

How to Associate a Content Types to SharePoint Library or List?

To add the content type to a list or Library in SharePoint, do the following:

  1. Go to the List / Library settings. 
  2. Click on Advanced Settings under the General Settings group.
  3. On the content types option, set it to “Yes” to allow management of content types.
  4. Now, on the list settings page, you’ll find a new section, “Content types,” with all associated content types in the list.
  5. You can click the “Add from existing site content types” link to add any existing content type to the list.
    sharepoint powershell add content type to list
  6.  On the “Add Content Types” page, in the “Available Site Content Types” section, select the content types you want to add to the list or library, click Add, and then OK. Now the new content type appears on the settings page.
    sharepoint 2013 powershell add content type to list
Content types and site columns are hierarchically inherited! So, if you create a content type, it will be available to all of its subsites underneath it!

Add a content type to the SharePoint list using PowerShell:

If you want to associate a content type to multiple lists on multiple sites, you can use the below PowerShell scripts to do it programmatically! In my case, I have a site with 50+ subsites. The requirement is to add a content type to a particular library on all sites. So, I wrote this PowerShell custom function to add remove content type in the SharePoint library.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Custom PowerShell Function to Add Content type to SharePoint list
Function Add-ContentTypeToList([Microsoft.SharePoint.SPList]$List, $ContentTypeName)
{
    #Check if Content Type Management is enabled
    if($List.ContentTypesEnabled -ne $true)
    {
        Write-Host "Enabling Content type management on list: $($list.Title)"
        $List.ContentTypesEnabled = $true
        $List.Update()
    }

    #Get the content type
    $ContentType = $List.ParentWeb.Site.RootWeb.ContentTypes[$ContentTypeName]

    #If content type found in the site
    if ($ContentType)
    {
        #Check If list doesn't has the specific content type already
        if (-not $list.ContentTypes[$ContentTypeName])
        {
            #Add content type to the list
            $list.ContentTypes.Add($ContentType) | Out-Null
           
            $list.Update()
            Write-Host "Content Type Added to the list: $($List.Title)"
        }
        else
        {
            Write-Host  "Content Type '$($ContentTypeName)' already exists on the list '$($list.Title)'"
        }
    }
    else
    {
        Write-Host  "Cannot find the Content Type '$($ContentTypeName)' to add to list '$($list.Title)'"
    }
}

#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposal"
$ContentTypeName ="Sales Proposal"

#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)

if($list -ne $null)
{
    #Call the function to add content type
    Add-ContentTypeToList $list $ContentTypeName
}

How to remove the content type from a list?

To remove any unused content types that you aren’t using, do the following:

  1. Browse to the List Settings or Library Settings page.
  2. Under the “Content Types” section, click the content type you want to remove.
  3. Click the “Delete This Content Type” link and then click OK at the confirmation prompt.

Remove a content type from the SharePoint list using PowerShell

Use this PowerShell script to delete a content type from a SharePoint List or document library:

#Custom PowerShell Function to Remove Content type from SharePoint list
Function Remove-ContentTypeFromList([Microsoft.SharePoint.SPList]$List, $ContentTypeName)
{
    #Check if Content Type Management is enabled
    if($List.ContentTypesEnabled -ne $true)
    {
        Write-Host "Enabling Content type management on list: $($list.Title)"
        $List.ContentTypesEnabled = $true
        $List.Update()
    }

    #Get the content to remove from list
    $ContentType = $List.ContentTypes[$ContentTypeName]

    #If content type found in the site
    if ($ContentType)
    {
        #Check If list doesn't has the specific content type already
        if ($list.ContentTypes[$ContentTypeName])
        {
            #Remove content type from the list
            $list.ContentTypes.Delete($ContentType.Id)
            $list.Update()
            Write-Host "Content Type has been removed from the list: $($List.Title)"
        }
        else
        {
            Write-Host  "Content Type '$($ContentTypeName)' doesn't exists on the list '$($list.Title)'"
        }
    }
    else
    {
        Write-Host  "Cannot find the Content Type '$($ContentTypeName)' to add to list '$($list.Title)'"
    }
}

#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposal"
$ContentTypeName ="Sales Proposal"

#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)

if($list -ne $null)
{
    #remove content type from list, call: 
    Remove-ContentTypeFromList $list $ContentTypeName
}

You can’t remove a content type if items are already created based on the content type! If you try to delete a content type in use, you’ll end up with “Content Type is still in use” error!

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!

2 thoughts on “Add-Remove Content Types to SharePoint List or Library using PowerShell

  • Could you explain line 47? I am trying to recreate this script for document libraries and it keeps saying that it cannot call a method on a null-valued expression. Is there another way to obtain a list of document libraries from a site?

    Reply
    • The TryGetList method tries to get a specific list from the given web. You can use $Web.Lists[“List Name”] as well, But the difference is the first method doesn’t trigger an error if the specific list name doesn’t exist.

      Reply

Leave a Reply

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