Update Created, Created by, Modified & Modified fields using SharePoint Online Powershell

Published: Updated: 3 min readPowerShell, SharePoint Online
PowerShell to update metadata fields of a list item

You have noticed that when you are migrating content in a SharePoint Document library or list using a migration tool and gives you the option to keep the timestamp and user details for created and modified fields. But if you manually copy few the fields than it would be showing up with you name and recent date.

Using SharePoint Online PowerShell

You can use SharePoint Online or PnP PowerShell cmdlets to update the date and user details for Create and modify fields for a SharePoint list item or document. I have written a PowerShell function UpdateTimestamp which will take site url, list name and create & modified parameter details from the user.

###########################################################################
#Author: Adnan Amin
#blog: Https://mstechtalk.com
#Update Created, Created by, Modified & Modified fields using SharePoint Online Powershell
###########################################################################

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"

#this function will require site url, list name, list item id which need to be updated and timestamp & user details for created and modified fields
function UpdateTimestampandUser($siteurl, $listname, $itemid, $createdbyUser, $modifedByUser, $createdDate, $modifiedDate)
{
    try{
    
        $cred = Get-Credential
        $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.Username, $cred.Password)
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
        $ctx.Credentials = $credentials
 
        $eCreatedByUser = $ctx.Web.EnsureUser($createdbyUser)
        $ctx.Load($eCreatedByUser)
        $eModifedByUser = $ctx.Web.EnsureUser($modifedByUser)
        $ctx.Load($eModifedByUser)
 
        $list=$ctx.Web.Lists.GetByTitle($listname)
        $item = $list.GetItemById($itemid) 
        $ctx.Load($item)
 
        #Below Powershell will update the user details for created and modified columns
        $item["Author"] = $eCreatedByUser
        $item['Editor'] = $eModifedByUser
 
        #Below Powershell will update the user details for created and modified columns
        $item["Created"] =  $createdDate
        $item["Modified"] = $modifiedDate
 
        $item.Update()
        $ctx.ExecuteQuery()

        Write-host "All fields updated Successfully!" -f Green
     }
     catch{
        Write-Host $_.Exception.Message -ForegroundColor Red
     }
}

#Function Call
UpdateTimeStampandUser -siteurl "https://mstalk.sharepoint.com" -listname "Documents" -itemid "4" -createdbyUser "demo@mstechtalk.com" -modifedByUser "spadmin@mstechtalk.com" -createdDate "2019/15/04 12:15:00 AM" -modifiedDate "2019/15/6 04:30:00 PM"

Using Pnp PowerShell

You can also achieve the same task by a simple PnP PowerShell cmdlet as listed below:

Set-PnPListItem -List "Documents" -Identity "4" -Values @{"Created"="demo@mstechtalk.com";"Modified"="spadmin@mstechtalk.com"; "Author"= "2019/15/04 12:15:00 AM"; "Editor"= "2019/15/6 04:30:00 PM"} 

I would not recommend you to update these values if there is no need, it will only be required if you need to maintain the original details of the list item. You can also iterate the above script if there are more list items with a little modification.

Share:
Adnan Amin

Adnan Amin

Adnan Amin is a Microsoft MVP for Microsoft 365 (since 2015) and a Senior Microsoft Consultant. He writes practical guides and PowerShell scripts for SharePoint, OneDrive, Teams and Copilot admins.

More posts by Adnan Amin

Leave a Reply

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