One of our users came up with a request to exchange all the old content types with the new one.
If you Google it you will find lots of resources which can help you achieve it.
But our user had a special request while changing the content type, the modified by or modified time should not be changed.
Below is the PowerShell to achieve it.
- Add - PSSnapin Microsoft.SharePoint.PowerShell# Custom PowerShell Function to
- switch Content type of all items stored in a SharePoint library
- Function Change - ItemContentType {
- param(
- [Microsoft.SharePoint.SPWeb] $Web = $(
- throw "Web URL!"), [string] $ListName = $(
- throw "ListName"), [string] $OldContentTypeName = $(
- throw "OldContentTypeName"), [string] $NewContentTypeName = $(
- throw " 'NewContentTypeName"))# Get the list
- $List = $Web.Lists[$ListName]# Get the new content type
- $NewContentType = $List.ContentTypes[$NewContentTypeName]# Iterate through each item in the list
- foreach($item in $list.Items) {
- if ($item.ContentType.Name - eq $OldContentTypeName) {#
- Change the content type
- $item["ContentTypeId"] = $NewContentType.Id
- $item.SystemUpdate()
- Write - host "Content type changed for item: "
- $item.id
- }
- }
- }#Variables
- for processing
- $WebURL = "Site URL"
- $ListName = "Library Name"
- $ContentTypeName = "New Content type Name"#Get the Web and List
- $Web = Get - SPWeb $WebURL
- $List = $Web.lists.TryGetList($ListName)
- if ($list - ne $null) {#Call the
- function to add content type
- Change - ItemContentType $web $list "Old Content type Name"
- "New Content type Name"
- }
Happy SharePointing.