4

Is there some way to change the content type of a file using PowerShell? Ideally I'd like to write a function that one would point at a specific document library and it would pull down an array of every file in the library then iterate through the list setting the content type of each file based on the file extension.

"This one's a .jpg? Mark it as 'Image'."
"This one's a .pdf? Mark it as 'PDF'."
"This one's a .txt file? Mark it as 'Document'."
"This one's a .png? Mark it as 'Image'."
"This one doesn't have an extension? Skip it."

I think you get my point.

3
  • do you mean a file-type field (column) that's part of your-document content type? May 4, 2012 at 20:28
  • @sbc111 - ...Can you be more specific?
    – newuser
    May 4, 2012 at 20:29
  • see post below.. May 4, 2012 at 20:44

3 Answers 3

10

Basically it's just

$item = $file.Item
$item["ContentTypeId"] = $ct.Id
$item.Update()

For an example with more details see Change the content type set on files in SharePoint using PowerShell

1
  • I'm marking this as the answer because I was able to modify the script at your link to suit my needs.
    – newuser
    May 7, 2012 at 17:02
2

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
                    }
0

you could inherit the base Document type into your-content type and include a column (field) called file-type.. file-type would contain the values - 'Image', 'PDF', etc.. Of course, you can also determine the values by reading the file-extension of the document when you are iterating through the libraries & list items.. here's a link that may be helpful - Adding Document Content Types – Sharepoint 2010

UPDATE: here's an example of iterating through a document library and checking each (SPFile) file's properties. SPFile has a property 'Name' which includes the file-extension. After determining the file-extension type (PDF, Image, etc) you can update 'file-type' column mentioned above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.