Thursday, 5 December 2013

Update the "Created By" "Modified By" and "Created" Columns in ShrePoint list using PowerShell

As a developer or administrator working with SharePoint, sometimes it is necessary to modify the 'Created' 'Created By' 'Modified' 'Modified By' columns where we can not modify in UI.

But using PowerShell easily you can modify these columns.
// Add only if you use windows PowerShell. 
Add-PSSnapin Microsoft.SharePoint.Powershell 

//Get a new object called $web to pick site 
$web=Get-SPWeb "URL" 

//Get a new object called $list to pick list
$list=$web.Lists["List Name"] 
Now you need to get the particular List Item ID, where you want to modify the columns.
//Get a new object called $item to pick Item
$item = $list.GetItemById("3214")
Now you need to get the columns names which you want to update. You can get the user ID of the user using the "EnsureUser()" method of "web" in PowerShell command.
$replacedUser =$web.EnsureUser("domainName\Account")
Note: The Author and Editor fields, these are the 'Created by' and 'Modified by' fields.
Author = Created by
Editor = 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 "URL" 
//Get a new object called $list to pick list
$list=$web.Lists["List Name"]
//Get a new object called $item to pick Item
$item = $list.GetItemById(3214)
$replacedUser =$web.EnsureUser("domainName\Account")
$item["Created"] = "25/2/2013 00:00:00"
$item["Modified"] = "25/8/2013 00:00:00"
$item["Author"] = $replacedUser
$item["Editor"] = $replacedUser
$item.Update();