Sometimes you need to lie to SharePoint. In this post i’ll show you how to change the details for who created an item, modified it and when they modified it.
When you’re doing bulk uploads, dealing with lists where you wish to use the Advanced features of only allowing users to edit their own items or just testing some behaviour, eventually you’ll wish you can change the values that SharePoitn doesn’t let you change.
The first thing is, as always, to find the value we want to change:
#Add the SharePoint snapin Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue #set the web url and the list name to work upon $url = "http://sharepoint/sites/cthub" $listName = "Shared Documents" $fileName = "FileName.xlsx" #Get the appropriate list from the web $web = get-SPWeb $url $list = $web.lists[$listName] #Get the file using the filename $item = $list.Items | ? {$_.Name -eq $fileName} #Print out current Created by and Created date Write-Output ("item created by {0} on {1}" -f $item["Author"].tostring(), $item["Created"] ) #Print out current Created by and Created date Write-Output ("item last modified by {0} on {1}" -f $item["Editor"].tostring(), ($item["Modified"] -f "dd-MM-yyyy"))
As you can see we access the item properties by treating the $item as a hashtable and use the property name as the key.
#Set the created by values $userLogin = "ALEXB\AlexB" $dateToStore = Get-Date "10/02/1984" $user = Get-SPUser -Web $web | ? {$_.userlogin -eq $userLogin} $userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring() #Sets the created by field $item["Author"] = $userString $item["Created"] = $dateToStore #Set the modified by values $item["Editor"] = $userString $item["Modified"] = $dateToStore #Store changes without overwriting the existing Modified details. $item.UpdateOverwriteVersion()
Setting the value is a bit more complicated. To do that you have to build the appropriate user string. In my example the user is already part of the site, if they haven’t previously been added to the user information list you’ll need an extra step here to insert them.
The second bit which differs from your usual PowerShell update is the use of the UpdateOverwriteVersion() method. There’s several update methods in SharePoint but only this one will preserve your changes to modified by and created by.
And now, the full script:
<# Title: Set modified and created by details Author: Alex Brassington Category: Proof of Concept Script Description This script is to show how to read, modify and otherwise manipulate the created by and modified by details on documents. This is to enable correction of incorrect data as part of migrations. It is also useful to enable testing of retention policies. #> #Add the SharePoint snapin Add-PSSnapin Microsoft.SharePoint.Powershell -ea SilentlyContinue #set the web url and the list name to work upon $url = "http://sharepoint/sites/cthub" $listName = "Shared Documents" $fileName = "FileName.xlsx" #Get the appropriate list from the web $web = get-SPWeb $url $list = $web.lists[$listName] #Get the file using the filename $item = $list.Items | ? {$_.Name -eq $fileName} #Print out current Created by and Created date Write-Output ("item created by {0} on {1}" -f $item["Author"].tostring(), $item["Created"] ) #Print out current Created by and Created date Write-Output ("item last modified by {0} on {1}" -f $item["Editor"].tostring(), ($item["Modified"] -f "dd-MM-yyyy")) #Set the created by values $userLogin = "ALEXB\AlexB" $dateToStore = Get-Date "10/02/1984" $user = Get-SPUser -Web $web | ? {$_.userlogin -eq $userLogin} $userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring() #Sets the created by field $item["Author"] = $userString $item["Created"] = $dateToStore #Set the modified by values $item["Editor"] = $userString $item["Modified"] = $dateToStore #Store changes without overwriting the existing Modified details. $item.UpdateOverwriteVersion()