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 set these values from the 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 the variables section appropriately and run the script.

I do not entertain manipulating these metadata fields, unless it is 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 = "https://crescent.sharepoint.com/sites/sales/"
$ListName= "Projects"
$ID=6
$UserID="[email protected]"
$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)
$ctx.Load($user)

#Get the list Item
$List=$Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ID) 
$Ctx.Load($ListItem)

#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
$ListItem.Update()
$ctx.ExecuteQuery()

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"

#Variables
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

#Define Metadata
$CreatedBy = "[email protected]"
$ModifiedBy = "[email protected]"
$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")
$Ctx.Load($File)
$Ctx.ExecuteQuery()

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

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

PnP PowerShell to Update List Item Metadata:
#Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Projects"
$ItemID = "2"

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

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

Set-PnPListItem -List $ListName -Identity $ItemID -Values @{"Created"=$Created;"Modified"=$Modified; "Author"= $CreatedBy; "Editor"= $ModifiedBy}

Similarly, to set metadata of existing file, use:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/branding"
$ListName = "Brand"
$SiteRelativeURL = "/Brand/Technical Design.docx"
$CreatedBy= "[email protected]"
$Title = "New Design Document V2.docx"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
  
#Get the File from SharePoint
$File = Get-PnPFile -Url $SiteRelativeURL -AsListItem
  
#Update document properties
Set-PnPListItem -List $ListName -Identity $File.Id -Values @{"Title" = $Title; "Author" = $CreatedBy}
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

2 comments:

  1. Great its worked well.

    But i need to iterate through all the documents at library and update "Created By" and "Modified By" for specific authors only. Foe e.g. i need to update the "Created by" and "Modified by" fields where the author of the files is "[email protected]" only.

    Please help!!

    ReplyDelete
    Replies
    1. Sure, You can use:
      $ListItems = Get-PnPListItem -List $Listname -PageSize 2000 | where {$_.FieldValues.Author.Email -eq "[email protected]" -and $_.FileSystemObjectType -eq "File"}

      $ListItems | ForEach-Object {
      Set-PnPListItem -List $ListName -Identity $_Id -Values @{"Author" = "[email protected]"}
      }

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.