SharePoint Online: Update Created By / Modified By, Created At / Modified At Field Values using PowerShell

Requirement: Update 'Created By' column in SharePoint Online using PowerShell

At times, you may have to create list items or set existing item's metadata fields such as Created by, Modified by, Created at, Modified values to a specific user - time stamp. Say, You are importing or migrating data from a network file share to SharePoint online and you want to keep these metadata as same as the source content.

Because, SharePoint doesn't let you to set these values from web user interface, we've to use PowerShell. Here is my PowerShell script to change the details of who created an item, modified it and when they created and modified it. This script sets system fields (created, created by, modified, modified by) for a specific item in the list/library. Set the values in variables section appropriately and run the script.

I do not entertain manipulating these metadata fields, unless its really required!

SharePoint Online: Change Created By using PowerShell
PowerShell script to update Created by, Modified By, Created at, Modified at field values:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
##Variables for Processing
$SiteUrl = ""
$ListName= "Projects"
$TimeStamp = "2015/12/01 02:10:00 AM"

#Get Credentials to connect
$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = $Credentials

#Get the User
$User = $Ctx.Web.EnsureUser($UserID)

#Get the list Item
$ListItem = $List.GetItemById($ID) 

#update created by column sharepoint online powershell
$ListItem["Author"] = $User
#Update Modified By
$ListItem['Editor'] = $User

#Set Created on & Modified on Time values
$ListItem["Created"] =  $TimeStamp
$ListItem["Modified"] = $TimeStamp

#Update List item

Write-host "Metadata values updated Successfully!" -f Green
This PowerShell script sets item's metadata field values in SharePoint Online and the result goes here:
sharepoint online powershell to update created by modified by created at modified at values
For SharePoint On-premises, Use this PowerShell script: Update "Created By", "Last Modified" Metadata Fields of a List Item using PowerShell

Similarly, for documents, you can use the below code to update its metadata:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL = ""

#Define Metadata
$CreatedBy = ""
$ModifiedBy = ""
$CreatedOn = "01/01/2017"
$ModifiedOn ="01/01/2018"

#Get Credentials to connect
$Cred = Get-Credential -Message "Enter the Admin Credentials:"

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Get the File
$File =  $Ctx.Web.GetFileByServerRelativeUrl("/sites/marketing/Migrate/Active%20Projects/Requirements.docx")

#Get 'Created By' and 'Modified By' Users
$Author = $Ctx.Web.EnsureUser($CreatedBy)
$Editor= $Ctx.Web.EnsureUser($ModifiedBy)

#update Metadata of the File
$ListItem = $File.ListItemAllFields
$Listitem["Author"] = $Author
$ListItem["Editor"] = $Editor
$ListItem["Created"] =  $CreatedOn
$ListItem["Modified"] = $ModifiedOn

PnP PowerShell to Update List Item Metadata:
$SiteURL= ""
$ListName = "Projects"
$ItemID = "2"

$Created = "2018-01-01T12:00:00-00:00"
$Modified = "2018-02-02T10:30:00-00:00"
$CreatedBy= ""
$ModifiedBy= ""

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Created"=$Created;"Modified"=$Modified; "Author"= $CreatedBy; "Editor"= $ModifiedBy}
SharePoint Online: Update Created By / Modified By, Created At / Modified At Field Values using PowerShell SharePoint Online: Update Created By / Modified By, Created At / Modified At Field Values using PowerShell Reviewed by Salaudeen Rajack on November 30, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.