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, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply