How to create SharePoint site column using PowerShell + Content type

In this PowerShell SharePoint tutorial, we will discuss how to create SharePoint site column using PowerShell. We will also discuss how to create content type using PowerShell in SharePoint 2013, SharePoint 2016, or SharePoint 2019.

We will also see, how to change content type in SharePoint using PowerShell.

Automating deployment process will always improve productivity and can eradicate manual errors. In this article, I am enhancing our PowerShell knowledge by adding one more feather creating “Site Columns and Content Types”. This will definitely help while setting up a larger site collection with multiple site columns and content types.

Create SharePoint Site Column and Content Types using PowerShell

The below PowerShell script will create a new content type group, content type, Site column group with one site column in SharePoint.

try {
$url = "http://sharepoint13:12345/"
$web = get-spweb $url
$MSTechnologyCTName = "MSTechnology Content Type"
$columnName = "Technology"
$contentTypes = $web.AvailableContentTypes | Select-Object Name
$cntTypeflag = $false
# We are taking Item as parent content type to inherit from "0x01" is the ID for Item content type
$parentcntType = $web.contenttypes | where {$_.ID -eq "0x01"} | Select-Object Name
foreach ($contType in $contentTypes) {
if ($contType.Name -eq $MSTechnologyCTName) {
$cntTypeflag = $true
Write-Host "Content Type=" $contType.Name -foregroundcolor White -BackgroundColor Green
}
}

if (!$cntTypeflag) {
$MSTechnologyCTParent = $web.availablecontenttypes[$parentcntType]
$MSTechnologyCT = new-object Microsoft.SharePoint.SPContentType($MSTechnologyCTParent, $web.contenttypes, $MSTechnologyCTName)
$MSTechnologyCT.Group = "MSTechnology Content Types Group"
$web.ContentTypes.Add($MSTechnologyCT)
$web.fields.add($columnName, "Text", $false)
$MSTechnologyfield = $web.Fields.GetField($columnName)
$MSTechnologyfield.Group = "MSTechnology Site Columns Group"
$MSTechnologyfield.Title = $columnName
$MSTechnologyfield.Update()
$MSTechnology_fieldLink = New-OBject Microsoft.SharePoint.SPFieldLink($MSTechnologyfield)
$MSTechnologyCT.FieldLinks.Add($MSTechnology_fieldLink)
$MSTechnologyCT.Update()
}
else {
Write-Host "Content Type existed in site collection"
}
}
catch {
Write-Host "`nError:: $($_.Exception.Message)" -foregroundcolor red -BackgroundColor Yellow
}

Let us walk through the code Line by Line:

Line 1: As per the MS best practices, I have rapped my code inside a try-catch block

Line 2: Created a variable $url for site collection http://sharepoint13:12345

Line 3: We are creating object to site collection http://sharepoint13:12345

Line 4 to 7: I am creating variables for various values like name of the content type to be created, column name to be created, content type existence flag and then I am fetching all the existing content types in the site collection with the below line of code.

$web.AvailableContentTypes | Select-Object Name

Note: In this statement, I am selecting the name of a content type from the entire collection of properties

Line 9: While creating content type we must define the parent content type from which we are inheriting the new content type. In the below line I am getting the name of “Item” content type.

$parentcntType = $web.contenttypes | where {$_.ID -eq “0x01”} | Select-Object Name

You can see more about the parent content types and the hexadecimal formats in the MSDN article:

Line 10 to 15: I am looping through the content types and ensuring that “MSTechnology Content Type” does not exist in case if it exists I am showing an error message that it already exists.

Line 18 to 29: I am creating below artifacts:

  • Site column “Technology”
  • Site column group “MSTechnology Site Columns Group”
  • Site Content Type Group “MSTechnology Content Types Group”
  • Creating “MSTechnology Content Type”

Once you run the script you can verify the new artifacts in the site collection by navigating to Site Settings -> Site Columns.

Newly created site column & Group with PowerShell script:

create site column using powershell sharepoint
create site column using powershell sharepoint

Newly Created site content type & group with PowerShell script:

create content type using powershell sharepoint
create content type using powershell sharepoint

This is how we can create a site column and content type in SharePoint using PowerShell.

Read: SharePoint site columns

Change SharePoint content type using PowerShell

My client has created a site collection in SharePoint 2013 with more than 300 subsites and 500+ document libraries with default content types.

Now the business wants to streamline the metadata collecting from end-user by associating “MSTechnology Content Type” content type to “Finance Documents” library in all the subsites across a site collection.

Implementing this change manually is completely a layman and more cumbersome job.

As we all know that Microsoft had extended PowerShell abilities from SharePoint 2010 onwards to such an extent that we can avoid any amount of manual work by implementing scripts in PowerShell.

Read: Introduction to SharePoint content types

Change SharePoint content type using PowerShell

I have implemented the below script to accomplish the user requirement. What we should know in PowerShell:

  • Implementing recursive looping
  • Using where conditions

I have manually created a content type called “MSTechnology Metadata” at the site collection level and the rest has been taken care by the script:

You can use Windows PowerShell ISE or Visual studio code to write and debug the PowerShell script.

1.$site = Get-SPSite http://sharepoint13:12345
2.$site | Get-SPWeb -Limit all | ForEach-Object {
3.write-host “Checking site:”$_.Title
4.$_.Lists | where { $_.BaseTemplate -eq “DocumentLibrary” –and $_.Title -eq “Finance Documents”} | ForEach-Object {
5.write-host “Checking Document:”$_.Title
6.$_.ContentTypesEnabled = $true
7.$ctToAdd = $site.RootWeb.ContentTypes[“MSTechnology Content Type”]
8.$ct = $_.ContentTypes.Add($ctToAdd)
9.write-host “Content type” $ct.Name “added to library” $_.Title
10.$_.Update()
11.}
12.}
13.$site.Dispose()

Let us walk through the code line by line.

Line 1: $site = Get-SPSite http://sharepoint13:12345
Creating object to site collection.

Line 2: $site | Get-SPWeb -Limit all
In this line we are fetching all the sites in the site collection iteratively with the parameter “-Limit all”.

By specifying this parameter “-Limit all” PowerShell iteratively loop through all the sites in the site collection till the leaf node.

It shows the output as below:

Url
—
http://sharepoint13:12345
http://sharepoint13:12345/-1
http://sharepoint13:12345/SubSite Level-10-1
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-10-1/SubSite…
http://sharepoint13:12345/SubSite Level-1-1
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-1-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …
http://sharepoint13:12345/SubSite Level-2-1/SubSite …

Etc will list all the sites till leaving node.

Line 4: $_.Lists | where { $_.BaseTemplate -eq “DocumentLibrary” –and $_.Title -eq “Finance Documents”}

Here I am querying the result set to get all the lists whose base template is “Document library” and Title “Finance Documents” to associate “MSTechnology Content Type” content type in the site collection till the leave node.

Line 7: $_.ContentTypesEnabled = $true

This enables allow content types property for the document library. This we can see as part of Advanced properties in a library setting.

Line 8: $ctToAdd = $site.RootWeb.ContentTypes[“MSTechnology Content Type”]

Creating object of the content type to be attached to the library

Line 9: $ct = $_.ContentTypes.Add($ctToAdd)

Attaching content type to the library.

Line 15: $site.Dispose()

To avoid memory leaks I am closing the site collection object.

The power of this script is, it updated Content Type of 100 document libraries in 4 minutes.

Change content type using PowerShell SharePoint 2013
Change SharePoint content type using PowerShell

This is how to change content type using PowerShell in SharePoint 2013/2016 or SharePoint 2019.

You may like following PowerShell SharePoint tutorials:

This SharePoint 2013 tutorial helps us to create a site column, content type using PowerShell in SharePoint 2013/2016.

>