SharePoint Online: PowerShell to Get a Content Type

Requirement: Get Content Types in SharePoint Online using PowerShell

PowerShell to Get Content Type by Content Type ID:

You can use PowerShell to get a content type by its Name or ID, or even all the content types in your site. In this blog post, we will show you how to use PowerShell to get a content type in SharePoint Online.

#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"

#parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID="0x01002A7A908ACAB0054880702EE263AC762B"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
#Get content type by ID
$ContentType = $Ctx.web.ContentTypes.GetById($ContentTypeID)
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
 
If($ContentType -ne $Null)
{
    #Get Columns from the content type
    $Ctx.Load($ContentType.Fields)
    $Ctx.ExecuteQuery()

    #Get columns from the content type
    ForEach($Field in $ContentType.Fields)
    {           
        Write-Host -f Green $Field.Title
    } 
}
else
{
    Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
}

SharePoint Online: PowerShell to Get Content Type by Name

Here is how to get content type by name in SharePoint Online using PowerShell:

#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 Get-SPOContentTypes()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ContentTypeName
    )
 
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #Get content types from web
        $ContentTypes = $Ctx.Web.ContentTypes
        $Ctx.Load($ContentTypes)
        $Ctx.ExecuteQuery()
 
        #sharepoint online powershell get content type by name
        $CType = $ContentTypes | Where {$_.Name -eq $ContentTypeName}
        If($CType -ne $Null)
        {
            #Get Columns from the content type
            $Ctx.Load($CType.Fields)
            $Ctx.ExecuteQuery()
 
            $ResultArray = @()
            #Loop through the Fields in the Content Type
            ForEach ($Field in $CType.Fields)
            {
                #Create a new custom object to hold our row of data with property names:
                $Result = New-Object PSObject
                $Result | Add-Member -MemberType NoteProperty -Name "Title" -Value $Field.Title
                $Result | Add-Member -MemberType NoteProperty -Name "Internal Name" -Value $Field.InternalName
                $Result | Add-Member -MemberType NoteProperty -Name "ID" -Value $Field.Id
                $Result | Add-Member -MemberType NoteProperty -Name "Group" -Value $Field.Group
                $Result | Add-Member -MemberType NoteProperty -Name "Description" -Value $Field.Description
 
                #Add the object to array
                $ResultArray += $Result
            }
            $ResultArray | Format-table -AutoSize
        }
        else
        {
            Write-host "Content Type '$ContentTypeName' doesn't exist!'" -f Yellow
        }
   }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL ="https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName="Announcement"

#Call the function
Get-SPOContentTypes -SiteURL $SiteURL -ContentTypeName $ContentTypeName

SharePoint Online: PowerShell to List All Content Types

Let’s get all content types from a SharePoint Online site using PowerShell

#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"
 
#Config Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
 
Try {
    #Get Credentials to connect
    $Cred= Get-Credential
 
    #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 content types from the web
    $ContentTypes = $Ctx.Web.ContentTypes
    $Ctx.Load($ContentTypes)
    $Ctx.ExecuteQuery()
  
    #List Content Type Name, Description, ID and Group
    $ContentTypes | Select Name, Description, Id, Group | Format-List  
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell to Get a Content Type by Name

To get a content type by its name, use this PnP PowerShell:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ContentTypeName ="Crescent Project Proposal V1"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName
 
#Get Content Type ID
$ContentType.Id.ToString()

Similarly, to get a content type by its ID, use:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ContentTypeID ="0x0104004A217DA260E04940AC9DB4A010797423"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeID
 
#Get Content Type Name
$ContentType.Name

Have you ever needed to get a list of all the content types in your SharePoint Online site? I’ll explain how to use PowerShell to get information about content types in SharePoint Online:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all site content types
$ContentTypes = Get-PnPContentType

#Get Content Type Name and ID
$ContentTypes | Select Name, ID

This PowerShell lists content types from SharePoint Online site. Here is another post to get all content types from the list: SharePoint Online: PowerShell to Get List Content Types

To export all content types to CSV or HTML report using PowerShell, refer: generate a report: SharePoint Online: Get Content Types Report using PowerShell 

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

5 thoughts on “SharePoint Online: PowerShell to Get a Content Type

  • Hi. In the Content type hub, it says the content type is published. I can add the content type manually to a list on the site through list settings. Once I do that, the content type does show when I run the script.

    How do I check if the site subscribes to the Content Type hub? I thought it automatically subscribes. Thanks

    Reply
  • Hi. Thanks for the reply. I ran the script and it does not list any of the content types that are hub content types.

    Reply
    • That hints either the content type is not published in the Content type hub or the site is not subscribed to receive the content types from the Content Type Hub.

      Reply
  • Hi. I am having an issue getting the content type that was published from a content type hub in SharePoint online.
    When I run the statement

    $ContentType = Get-PnPContentType -Identity content type name”

    I get the error Content type “xxxx” not found in site

    I see the content type in the Site Content Types page in site settings. If I manually add the content type to a library the statement works.

    Can you please assist. Thanks.

    Reply
    • Can you check if your content type is retrieved with this PowerShell script:

      #Get all site content types
      $ContentTypes = Get-PnPContentType

      #Get Content Type Name and ID
      $ContentTypes | Select Name, ID

      Reply

Leave a Reply