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.

Adnan is six time Microsoft MVP (Since 2015) with over 16 years of extensive experience with major expertise on SharePoint, SharePoint based development, Microsoft 365, Microsoft Teams, .Net Platform and Microsoft BI. He is currently working Sr Microsoft Consultant at Olive + Goose. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past 12 years and worked on different intranet/intranet solutions for private & govt. sector majorly in United states and Gulf region and have experience of working with multiple Fortune 500 companies. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply