Friday, February 13, 2015

Update List Item Metadata Fields (Created By, Last Modified) using PowerShell

Ever wanted to update SharePoint list or library item's metadata fields such as: "Created By" "Modified By" "Created" "Last Modified"? These column values must be updated while copying items from one list to another, programmatically.

Today, Had an another requirement to update metadata fields in a document stored in SharePoint document library. Lets update these metadata fields such as "Created By" "Modified By" "Created" "Last Modified" using PowerShell.

PowerShell Script to Update Metadata Fields in SharePoint:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
 
#Define the Parameters
$LibraryName = "Project Documents"
$DocumentName = "Project Schedules.xlsx"
$UserName = "Global\EricCo"
$DateValue = "2013-01-01 10:05" # Should be in "yyyy-mm-dd HH:mm" format
 
#Get the Web
$web= Get-SPWeb $WebURL
#Get the Library
$list= $web.Lists[$LibraryName]
#Get the User
$Author =$web.EnsureUser($UserName)
#Get the document
$Document = $list.Items | where {$_.Name -eq $DocumentName}
 
#update created by column sharepoint programmatically
$Document["Author"] = $Author
#set modified by programmatically
$Document["Editor"] = $Author
 
#Set Created Date value
$Document["Created"] =  $DateValue
#Set Last Modified Date
$Document["Modified"] = $DateValue
 
$Document.UpdateOverwriteVersion() #Can use $Document.SystemUpdate() as well..




Read more: http://www.sharepointdiary.com/2014/03/update-created-by-last-modified-metadata-fields-using-powershell.html#ixzz3Re4mrBqI

No comments:

Post a Comment