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

Requirement: Update the ‘Created By’ column in SharePoint Online using PowerShell.

Sometimes, you may have to create list items or set existing item’s metadata fields such as Created by, Modified by, Created, and Modified values to a specific user and time stamp. Say, You are importing or migrating data from a network file share to SharePoint Online, and you want to keep this 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="Salaudeen@crescent.com"
$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 = "salaudeen@crescent.com"
$ModifiedBy = "salaudeen@crescent.com"
$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= "Salaudeen@crescent.com"
$ModifiedBy= "Salaudeen@crescent.com"

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

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= "Salaudeen@crescent.com"
$Title = "New Design Document V2.docx"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
  
#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://Crescent.sharepoint.com/sites/Sales"
$ListName = "Documents"
$FolderServerRelativeURL = "/sites/Sales/Shared Documents/Samples"

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

#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" = "Steve@Crescent.com"; "Editor"="Steve@Crescent.com" } | 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 = "Salaudeen@crescent.com"
$ModifiedBy = "Salaudeen@crescent.com"
$Created = "2018-01-01T12:00:00-00:00"
$Modified = "2018-02-02T10:30:00-00:00"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
   
#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 - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

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

  • I want to set the Modified date of an existing file in Sharepoint online.
    Trying to use the example above:
    Set-PnPListItem -List $ListName -Identity $ItemID -Values @{“Modified”=$Modified}

    But doesn’t work. I can set other properties, but the Modified date seems to be controlled by Sharepoint.

    Is there a way to set it?

    Reply
  • 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
    • Looks its a known issue! You can’t update both Created and Modified at the same time (in one execution).

      Reply
  • 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
  • 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 = “abc@abc.com”
    Set-PnPListItem -List $ListName -Identity $_.Id -Values @{“Author” = $CreatedBy; }

    Reply
  • This comment has been removed by the author.

    Reply
  • 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
  • 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
  • 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
    • Try the -SystemUpdate switch in Set-PnPListItem. If CSOM, use: Item.SystemUpdate()

      Reply
  • 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
  • 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
    • 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” = “Steve@Crescent.com”;}

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

    Reply
  • 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 “abc@xyz.com” only.

    Please help!!

    Reply
    • Sure, You can use:
      $ListItems = Get-PnPListItem -List $Listname -PageSize 2000 | where {$_.FieldValues.Author.Email -eq “abc@xyz.com” -and $_.FileSystemObjectType -eq “File”}

      $ListItems | ForEach-Object {
      Set-PnPListItem -List $ListName -Identity $_Id -Values @{“Author” = “xyz@abc.com”}
      }

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *