Home > PowerShell, SharePoint 2010 > Find and Replace Content Types in SharePoint 2010 using PowerShell

Find and Replace Content Types in SharePoint 2010 using PowerShell

If you ever needed to replace a specific content type in your SharePoint 2010 site but you do not want to go through every single document library manually to remove and assign a new content type, you can accomplish this by turning to PowerShell.

Below is the script I used to iterate through a site collection and sub-sites, check the content type of each document library and if it was a specific content type , removes it and replaces it with the correct content type.

Copy the script below, change the parameters in “Red” to reflect your environment, save the file on your SharePoint Server.

1.  Log into your SharePoint (CA) Server
2.  Bring up SharePoint Management Shell (PowerShell) with Admin priviliages
3.  Browse to the location you saved the script
4.  Exectue script (.\nameofscript.ps1)

================================================================================

#Get Site Collection
$site = Get-SPSite http://portalsite
 
#Create CSV header
“siteURL `t” + “listName `t” + “listURL `t” + “ContentType `t” + >> Content-Types.csv

#Get Site Collection Webs
$webs = $sites.AllWebs

#Loop through each web in Site Collection
foreach($web in $webs)
{

Write-Host “Checking Sites: ” $web.Title

#Loop through all Site List Content Types
foreach ($ctype in $lst.ContentTypes)
{
#If Content Type equals a specific content type
if ($ctype.Name -eq “NAME OF CONTENT TYPE YOU WANT TO REMOVE“)
{

Write-Host “Content type Document exists on: ” $lst.Title
$web.Url + “`t” + $lst.title + “`t” + $lst.DefaultViewUrl + “`t” + $ctype.Name >> Content-Types.csv

#if ContentTypes Setting is not enabled on the Site, enable them
if ($lst.ContentTypesEnabled -eq $false)
{

#Enable ContentTypes on the site level
$lst.ContentTypesEnabled = $true
$lst.Update()

}

#Remove contentTypeNAME OF CONTENT TYPE YOU WANT TO REMOVEfrom list

Write-host “Removing Content Types ” $ctype.Name  “from ” $lst.Title
$ctToRemove = $lst.ContentTypes[“NAME OF CONTENT TYPE YOU WANT TO REMOVE“]
$lst.ContentTypes.Delete($ctToRemove.Id)
$lst.Update()

#Add Site contentTypeNAME OF CONTENT TYPE YOU WANT TO ADDto list

Wrtie-Host “Adding Content Type NAME OF CONTENT TYPE YOU WANT TO ADD” $lst.Title
$ctToAdd = $sites.RootWeb.ContentTypes[“NAME OF CONTENT TYPE YOU WANT TO ADD“]
$lst.ContentTypes.Add($ctToAdd)
$lst.Update()
}

}

}
$site.Dispose()

Advertisements
  1. Raghav
    March 20, 2013 at 4:16 am

    Hi, This post is helpful. However, can you tell what can be modified in this code to check for all the content types that are only being used in various sites on my webapp? please let me know.

  2. April 4, 2013 at 3:59 pm

    Hi, thanks so much for the script!

    Question- Does this automatically convert all documents that currently adhere to the content type being replaced?

    • April 8, 2013 at 10:36 am

      Hi Lynne,

      Unfortunately this script only changes the ContentType associated with the Document library, not the items. I have another script that I will be blogging about here shortly that will update each item in the list with the new ContentType.

  3. April 8, 2013 at 4:14 pm

    Cool, sounds great! Thanks for the prompt response!

  4. April 9, 2013 at 4:42 pm

    One more question if you don’t mind…seeming to get a strange error with this script:

    Exception calling “Add” with “1” argument(s): “Could not find file ‘C:\Users\SP
    Admin\AppData\Local\Temp\fj961xhv.dll’.”
    At C:\scripts\replacedoc.ps1:39 char:22
    + $lst.ContentTypes.Add <<<< ($ctToAdd)
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation

    Anything you've seen before or can identify?

    Thanks!

    L

  5. SP Geek
    February 21, 2015 at 8:32 am

    If you try to replace the content type of existing items by removing content type, You’ll end-up with “Content type still in use” Error. So, Instead of Removing, You got to “Update” the content type as in How to change the content type of Existing Items in SharePoint

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: