Using PowerShell to retrieve Content Types and associated Fields

I’ve previously posted PowerShell scripts that allow you to extract Content Types or Fields from a SharePoint site. But last week a commenter by the named Jeff posed a valid question. What if I want to extract all Content Types in a SharePoint site, and show the associated Fields for each of them?

While my previous scripts does not allow for that, it wasn’t all that hard to combine the two scripts into one. Over the weekend I quickly threw something together and the result is a PowerShell script that iterates through each Content Type and prints out each Field it finds. It’s pretty simple. I’ve posted the script below, and I’ll also post a truncated version of the output for 2007 and 2010 in later blog posts.

# Description
#   Output all available Content Type GUIDs and their respective fields
#
# Syntax
#   ./listCtFields
#
# Parameters
#   none
#
# Settings
#   Only change the -value parameter!
#
set-variable -option constant -name url -value "http://localhost"  # Site collection
set-variable -option constant -name out -value "ContentTypes.csv"  # Site collection
# End of settings

$site = new-object Microsoft.SharePoint.SPSite($url)
$cts = $site.rootweb.ContentTypes
echo "Processing..."

'"CT Name"' + `
',"CT ID"' + `
',"CT Description"' + `
',"CT Group"' +
',"Field Title"' + `
',"Field Internal Name"' + `
',"Field ID"' + `
',"Field Group"' + `
',"Field Max Length"' + `
',"Field Description"' | Out-File $out

ForEach ($id in $cts)
{
  ForEach ($field in $id.Fields)
  {
    '"' + $id.Name + `
    '","' + $id.Id + `
    '","' + $id.Description + `
    '","' + $id.Group + `
    '","' + $field.Title + `
    '","' + $field.InternalName + `
    '","' + $field.Id + `
    '","' + $field.Group + `
    '","' + $field.MaxLength + `
    '","' + $field.Description + `
    '"' | Out-File $out -append
  }
}

$site.Dispose()

echo "Finished!"

# Changelog
#
#   v1.0 - February 28, 2011
#       First release

12 comments

    • On the very first set-variable line, change “http://localhost” to the URL of the site collection you wish to extract the content types from. Then run the script on a server in your farm. It should spit out a csv file containing all the information.

  1. Hi,
    Great Script. I have a need to insert content types. Is there a way to insert Content Types/Fields using PowerShell into Sharepoint 2007 (MOSS)?

    Thank You.

    • PowerShell is a separate install on MOSS2007. Just download it, install, and the script should run just fine. That’s what I did for the 2007 version of my data dump.

  2. Hi,

    Can you tell me if it is also possible to pull a specific attribute of a content type using your script (i.e. the Inherits = True/False attribute)? How would this be done?

    Cheers,
    Aaron

    • I don’t see why not… In the inner for loop just put an if statement checking whether a certain field matches your expectations, when only write out the content type if it does.

  3. Hello,
    Is there a way to know in which site a specific content type is located in a web application using PowerShell? There lots of site collection in my web application so It is very hard to find a content by go into each site. I can extract the list of content types in my web application with their IDs using PowerShell but I don’t know how to extract the source/ location/URL of the sub site where that specific content type is located.

    Please help!

Leave a Reply to Michael Tyson Cancel reply

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