Sometimes, it is necessary to modify the 'Created By' field value for each list item and set it to the respective user's login ID. In my case, it was required to Update "Created By" field value of Comments List of Blog Site. There were 1200+ comments in the list to modify.
The example simply replaced the created by field with user X to user Y. Apparently, you also need to specify the user ID during replace Created by or Modified by.
// Add only if you use Windows powershell.
Add-PSSnapin Microsoft.SharePoint.Powershell
//Get a new object called $web to pick site
$web=get-SPWeb "You Web URL"
//Get a new object called $web to pick site
$list=$web.Lists["Your List"]
You can get the user ID of the user using the "EnsureUser()
" method of "web
" in PowerShell command.
$replacedUser =$web.EnsureUser("domainName\Account")
The code "$item["Author"] = $replacedUser
" is used to updated the "Created By" column.
If you want to update the "Modified By" column, you have to add the code below:
"$item["Editor"] = $replacedUser "
foreach($item in $list.Items)
{
$item["Author"] = $replacedUser
$item["Editor"] = $replacedUser
$item.update()
}
$web.Update()
$web.dispose()
Don't forget to use $item.update()
at the end, because otherwise your new value won't get displayed.
Reference