You also put up two extra conditions if the file is already Checked-in or Checked-out.
For already checked-in files you can simply change their Content-Types and for checked-out ones you can Override the Check-out and then change the Content-Type.
# Check the check out status of the file
if ($_.File.CheckOutType -eq "None")
{
# Change the content type association for the item
$newCT1 = $list.ContentTypes[$newCType]
$newCTID = $newCT1.ID
$item = $_.file
$item.CheckOut()
write-host "Resetting content type for file" $_.Name "from" $oldCT "to" $newCType -foregroundColor Green
$_["ContentTypeId"] = $newCTID
$_.Update()
$item.CheckIn("Content type changed to " + $newCT1.Name, 1)
# Output results to file
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $oldCT, $newCT1.Name, $item.Name, $ctypeChanged
$outstring -join "," >> $filePath
}
else
{
write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
$checkOutUser = $_.File.CheckedOutByUser.ToString()
# Override Checkout
$_.File.UndoCheckOut()
write-host "Checkout overriden" -foregroundcolor Yellow
# Change the content type association for the item
$newCT1 = $list.ContentTypes[$newCType]
$newCTID = $newCT1.ID
$item = $_.file
$item.CheckOut()
write-host "Resetting content type for file" $item.Name "from" $oldCT "to" $newCT1.Name -foregroundcolor Cyan
$_["ContentTypeId"] = $newCTID
$_.Update()
$item.CheckIn("Content type changed to " + $newCT1.Name, 1)
# Output results to file
write-host "File" $item.Name "has been overwritten and content type changed to" $newCT1.Name -foregroundcolor Yellow
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $oldCT, $newCT1.Name, $item.Name, $ctypeChanged, $checkOutUser
$outstring -join "," >> $filePath
}