Change Content Type With PowerShell

Objective:

Change the content type of many files in a single library.

Solution:

Use the following PowerShell script to change the content type instead of editing each file one at a time. In my example, I change the content type from “Document” to “Process”.


function Reset-SPFileContentType ()
{
#Get web, list and content type objects
$web = Get-SPWeb -Identity http://spsite
$list = $web.Lists["Documents"]
$oldCT = $list.ContentTypes["Document"]
$newCT = $list.ContentTypes["Process"]
$newCTID = $newCT.ID

#Check if the values specified for the content types actually exist on the list
if (($oldCT -ne $null) -and ($newCT -ne $null))
{
#Go through each item in the list
$list.Items | ForEach-Object {
#Check if the item content type currently equals the old content type specified
if ($_.ContentType.Name -eq $oldCT.Name)
{
#Check the check out status of the file
if ($_.File.CheckOutType -eq "None")
{
#Change the content type association for the item
$_.File.CheckOut()
write-host "Resetting content type for file" $_.Name "from" $oldCT.Name "to" $newCT.Name
$_["ContentTypeId"] = $newCTID
$_.Update()
$_.File.CheckIn("Content type changed to " + $newCT.Name, 1)
}
else
{
write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
}
}
else
{
write-host "File" $_.Name "is associated with the content type" $_.ContentType.Name "and shall not be modified"
}
}
}
else
{
write-host "One of the content types specified has not been attached to the list"$list.Title
}
$web.Dispose()
}
Reset-SPFileContentType

Credit to Phil Childs’ article for SharePoint 2010. The script also works for SP 2013.