This post is the second in a series detailing the creation of SharePoint Site Columns and Content Types via PowerShell. The series has three parts as follows:

Part 1: Bulk Creating Site Columns with PowerShell

Part 2: Bulk Creating Content Types with PowerShell

Part 3: Bulk Adding Site Columns to Content Types with PowerShell

Bulk Creating Content Types with PowerShell: Overview
While not as tedious as Site Column creation, manual Content Type creation produces enough workload to demand attention. Whether creating Content Types in the Content Type Hub, or in a decentralized manner across Site Collections, the click count more than warrants using an automated solution via PowerShell.

Bulk Creating Content Types with PowerShell: The Logic
The script in the next section loops through a CSV file in order to determine key values for Content Type creation, such as Parent Content Type and Content Type Name. The ForEach-Object statement is used to get a row in the CSV and create a Content Type, before proceeding to the next row for processing. This reduces the speed of processing, but also reduces the amount of memory usage during the operation. The Site Collection URL is also processed as part of the loop in order to allow the creation of Content Types across Site Collections, although, most will likely target the Content Type Hub for central control of their Content Types.

Bulk Creating Content Types with PowerShell: The Script
The CreateContentTypes.ps1 script references a CSV file named CreateContentTypes. Instructions on the save location and execution of the files are included inline with the script. You will need to create a CSV file named "CreateContentTypes" with the column headings as listed in the following table.

Table 1: CreateContentTypes.csv Column Headings, Description, and Test Values

Column Heading Name

Column Heading Description

Column Heading Test Value

SiteCollectionURL

The Site Collection where the Content Type will be created

http://intranet.yourcompany.com

ParentContentType

The Parent Content Type the new Content Type will be based on

Document

NewContentType

The name of the Content Type

Test Document

Group

The group where the Content Type is referenced. If no group exists, it will be created

Test Content Types

Description

The description of the Content Type

This is a test Content Type


Open notepad and copy the following script into a text file named CreateContentTypes. Once the file is saved, rename the extension of the file to .ps1 to convert it into a PowerShell script. Please note the inline documentation for the save location and execution instructions.

<span class="co1"># CreateContentTypes.ps1</span>
<span class="co1"># </span>
<span class="co1"># Description: </span>
<span class="co1">#</span>
<span class="co1"># This script creates Content Types in the appropriate Site </span>
<span class="co1"># Collections as listed in the CreateContentTypes.csv. This </span>
<span class="co1"># includes creating Document Sets.</span>
<span class="co1"># The .CSV needs to be saved to "C:\PowerShell\" directory.</span>
<span class="co1"># If this directory does not exist, you will need to create it.</span>
<span class="co1">#</span>
<span class="co1"># Running this script requires running PowerShell with elevated </span>
<span class="co1"># privileges so right click the SharePoint 2010 Management Shell </span>
<span class="co1"># and select "Run as administrator" then use change directory </span>
<span class="co1"># commands and tabs to run the PS1 from its directory.</span>
 
 
<span class="co1"># Reference the CSV holding the Content Type values and begin the loop</span>
<span class="re0">$create</span> <span class="sy0">=</span> <span class="kw1">Import-Csv</span> <span class="kw5">-path</span> c:\PowerShell\CreateContentTypes.csv
<span class="re0">$create</span> <span class="sy0">|</span> <span class="kw1">ForEach-Object</span> <span class="br0">{</span>
 
<span class="co1"># Get the Site where the Content Type will be created</span>
<span class="re0">$site</span> <span class="sy0">=</span> Get<span class="sy0">-</span>SPSite <span class="sy0">-</span>Identity <pre class="de1"><span class="co1"># CreateContentTypes.ps1</span>
<span class="co1"># </span>
<span class="co1"># Description: </span>
<span class="co1">#</span>
<span class="co1"># This script creates Content Types in the appropriate Site </span>
<span class="co1"># Collections as listed in the CreateContentTypes.csv. This </span>
<span class="co1"># includes creating Document Sets.</span>
<span class="co1"># The .CSV needs to be saved to "C:\PowerShell\" directory.</span>
<span class="co1"># If this directory does not exist, you will need to create it.</span>
<span class="co1">#</span>
<span class="co1"># Running this script requires running PowerShell with elevated </span>
<span class="co1"># privileges so right click the SharePoint 2010 Management Shell </span>
<span class="co1"># and select "Run as administrator" then use change directory </span>
<span class="co1"># commands and tabs to run the PS1 from its directory.</span>
 
 
<span class="co1"># Reference the CSV holding the Content Type values and begin the loop</span>
<span class="re0">$create</span> <span class="sy0">=</span> <span class="kw1">Import-Csv</span> <span class="kw5">-path</span> c:\PowerShell\CreateContentTypes.csv
<span class="re0">$create</span> <span class="sy0">|</span> <span class="kw1">ForEach-Object</span> <span class="br0">{</span>
 
<span class="co1"># Get the Site where the Content Type will be created</span>
<span class="re0">$site</span> <span class="sy0">=</span> Get<span class="sy0">-</span>SPSite <span class="sy0">-</span>Identity #####replaceparse0#####lt;span class="br0">(</span><a><span class="kw6">$_</span></a>.<span class="st0">'SiteCollectionURL'</span><span class="br0">)</span>
<span class="re0">$web</span> <span class="sy0">=</span> <span class="re0">$site</span>.RootWeb
 
<span class="co1"># Determine available Content Types Parents and add new Content Types</span>
<span class="re0">$Web</span>.AvailableContentTypes <span class="sy0">|</span> <span class="kw2">Select</span> Name
<span class="re0">$parent</span> <span class="sy0">=</span> <span class="re0">$Web</span>.AvailableContentTypes<span class="br0">[</span><span class="st0">"$($_.'ParentContentType')"</span><span class="br0">]</span>
<span class="re0">$contentType</span> <span class="sy0">=</span> <span class="kw1">New-Object</span> Microsoft.SharePoint.SPContentType 
<span class="kw5">-ArgumentList</span> <span class="sy0">@</span><span class="br0">(</span><span class="re0">$parent</span><span class="sy0">,</span><span class="re0">$Web</span>.ContentTypes<span class="sy0">,</span><span class="st0">"$($_.'NewContentType')"</span><span class="br0">)</span>
<span class="re0">$contentType</span>.<span class="kw2">Group</span> <span class="sy0">=</span> <span class="st0">"$($_.'Group')"</span>
<span class="re0">$contentType</span>.Description <span class="sy0">=</span> <span class="st0">"$($_.'Description')"</span>
<span class="re0">$Web</span>.ContentTypes.Add<span class="br0">(</span><span class="re0">$contentType</span><span class="br0">)</span>
<span class="re0">$Web</span>.Update<span class="br0">(</span><span class="br0">)</span>
 
<span class="co1"># Dispose of the Web and Site objects and close the loop</span>
<span class="re0">$Web</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
<span class="re0">$site</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
<span class="br0">}</span></pre>lt;span class="br0">(</span><a><span class="kw6">$_</span></a>.<span class="st0">'SiteCollectionURL'</span><span class="br0">)</span>
<span class="re0">$web</span> <span class="sy0">=</span> <span class="re0">$site</span>.RootWeb
 
<span class="co1"># Determine available Content Types Parents and add new Content Types</span>
<span class="re0">$Web</span>.AvailableContentTypes <span class="sy0">|</span> <span class="kw2">Select</span> Name
<span class="re0">$parent</span> <span class="sy0">=</span> <span class="re0">$Web</span>.AvailableContentTypes<span class="br0">[</span><span class="st0">"$($_.'ParentContentType')"</span><span class="br0">]</span>
<span class="re0">$contentType</span> <span class="sy0">=</span> <span class="kw1">New-Object</span> Microsoft.SharePoint.SPContentType 
<span class="kw5">-ArgumentList</span> <span class="sy0">@</span><span class="br0">(</span><span class="re0">$parent</span><span class="sy0">,</span><span class="re0">$Web</span>.ContentTypes<span class="sy0">,</span><span class="st0">"$($_.'NewContentType')"</span><span class="br0">)</span>
<span class="re0">$contentType</span>.<span class="kw2">Group</span> <span class="sy0">=</span> <span class="st0">"$($_.'Group')"</span>
<span class="re0">$contentType</span>.Description <span class="sy0">=</span> <span class="st0">"$($_.'Description')"</span>
<span class="re0">$Web</span>.ContentTypes.Add<span class="br0">(</span><span class="re0">$contentType</span><span class="br0">)</span>
<span class="re0">$Web</span>.Update<span class="br0">(</span><span class="br0">)</span>
 
<span class="co1"># Dispose of the Web and Site objects and close the loop</span>
<span class="re0">$Web</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
<span class="re0">$site</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
<span class="br0">}</span>