SharePoint Online: Update Created By / Modified By, Created / Modified 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

To set metadata such as created, modified, created by and modified by field values, use this PnP PowerShell:

#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}

You can use it to update metadata for both list items and documents in SharePoint Online. 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; "Editor" = $CreatedBy}

To update Folder’s metadata values, use:

#Parameters
$SiteURL = "https://crescentintranet.sharepoint.com/sites/Sales"
$ListName = "Documents"
$FolderServerRelativeURL = "/sites/Sales/Shared Documents/Samples"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get the Folder
$Folder = Get-PnPFolder -Url $FolderServerRelativeURL -Includes ListItemAllFields

#Update Folder's Created By and Modified By Values
Set-PnPListItem -List $ListName -Identity $Folder.ListItemAllFields.Id -Values @{"Author" = "[email protected]"; "Editor"="[email protected]" } | Out-Null

Update Created By, Modified By for All Files in a Document Library

This time let’s update created by, modified by, created and modified field values for all files in a SharePoint Online document library.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$CreatedBy = "[email protected]"
$ModifiedBy = "[email protected]"
$Created = "2018-01-01T12:00:00-00:00"
$Modified = "2018-02-02T10:30:00-00:00"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
   
#Get all Files from the document library
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Fields Author | Where {$_.FileSystemObjectType -eq "File"}
   
#Update document properties
$ListItems | ForEach-Object { 
    Set-PnPListItem -List $ListName -Identity $_.Id -Values @{"Created"=$Created; "Modified"=$Modified; "Author" = $CreatedBy; "Editor" = $ModifiedBy; } | Out-Null
    Write-host "Metadata Set to "$_.FieldValues.FileRef -f Green
    }

This script scans and updates metadata of all documents stored in a library, including files from all folders.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

18 thoughts on “SharePoint Online: Update Created By / Modified By, Created / Modified Field Values using PowerShell

  • July 29, 2021 at 6:16 PM

    Figured it out… You can’t update the property for the “Label” and also update other values at the same time. You have to do this as two seperate actions.

    Reply
    • July 29, 2021 at 7:57 PM

      Looks its a known issue! You can’t update both Created and Modified at the same time (in one execution).

      Reply
  • July 28, 2021 at 7:55 PM

    Hiya, i’m not managing to get the -values parameters to work.

    Short background; Migrating from SP2013 to SPO and have a column which specifies the retention label which should be applied to files.

    I’m trying to: set the Label, Title and Modified date

    $folderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $targetFolderUrl -ItemType File

    foreach ($Item in $folderItems) {
    $date = Get-Date
    $file = Get-PnPFile -Url $Item.ServerRelativeUrl -AsListItem
    $fileName = $file.FieldValues.FileLeafRef -replace “.$($file.FieldValues.File_x0020_Type)”,””
    Set-PnPListItem -List “Shared Documents” -Identity $file.Id -Label “Working Document (Teams)” -Values @{“Modified” = $date; “Title” = $fileName }
    }

    Works for the setting of the label but doesn’t set the Date or Title, any thoughts/ideas would be really appreciated

    Reply
  • April 28, 2021 at 5:15 PM

    Hi Rajack, Is it possible to update SPO item CreatedBy with an orphaned username(not a valid domain user).I get error stating user not found when using this

    $CreatedBy = “[email protected]
    Set-PnPListItem -List $ListName -Identity $_.Id -Values @{“Author” = $CreatedBy; }

    Reply
  • February 17, 2021 at 6:09 PM

    This comment has been removed by the author.

    Reply
  • February 3, 2021 at 4:43 AM

    While Updating these metadata of “Site Pages” Item , they are getting changed But I’m again asked to republish the site page which will again change the “modified” and “Editor” to my email id. Is there a solution for that?

    Reply
  • November 10, 2020 at 6:41 PM

    The PNP script is not updating the Author. I am testing on a single document.

    Any idea what I am doing wrong?

    https://i.imgur.com/GLuH088.png

    Reply
  • July 9, 2020 at 4:50 PM

    Whenever I run a pnp script to update file columns, the “modified by” column is updated to the admin’s name (since the admin’s credentials are used in the script). Is there a way to override that so the “modified by” column is not changed when the script is finished? Help would be much appreciated!

    Reply
    • November 11, 2020 at 2:46 PM

      Try the -SystemUpdate switch in Set-PnPListItem. If CSOM, use: Item.SystemUpdate()

      Reply
  • July 6, 2020 at 8:14 PM

    Thank you for this great post!
    Is there anyway to update these columns using a csv file that has the correct time stamps and authors?
    Thanks!

    Reply
  • June 8, 2020 at 3:21 AM

    Thank you for you post.
    Please how can I update a folder modified by and created by fields in a document library. For example the folder is in another folder /Test1/news1/abc. Where abs is the folder to be updated

    Reply
    • June 10, 2020 at 2:34 PM

      Sure, You can use:
      $Folder = Get-PnPFolder -Url “/Test1/news1/abc” -Includes ListItemAllFields

      #Update Folder’s Created By Value
      Set-PnPListItem -List “Documents” -Identity $Folder.ListItemAllFields.Id -Values @{“Author” = “[email protected]”;}

      Reply
  • June 2, 2020 at 8:46 PM

    This is what I needed however, I need to be able to recurs into multiple folder levels. Any help would be appreciated.

    Reply
  • May 5, 2020 at 6:25 AM

    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!!

    Reply
    • May 5, 2020 at 12:53 PM

      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]”}
      }

      Reply

Leave a Reply