Updating SharePoint list item metadata manually can be a time-consuming process, especially when dealing with a large number of items, specially in case of content migration. This script automates the process, saving time and reducing the risk of errors.

In this blog post, we’ll explore a PowerShell script authored by Adnan Amin that updates the metadata of a SharePoint list item. Specifically, it modifies the “Created”, “CreatedBy”, “Modified”, and “ModifiedBy” fields.

Key Scenarios why we need to update list item metadata values

  1. Data Migration: During data migration, the metadata of SharePoint list items may need to be updated to match the new data source. This script can automate this process.
  2. Correcting Inaccurate Metadata: If the metadata of SharePoint list items is found to be inaccurate, this script can be used to correct it.
  3. Bulk Updates: If you need to update the metadata of multiple SharePoint list items, doing so manually would be time-consuming and error-prone. This script allows you to perform these updates quickly and accurately.

PowerShell script for list item Metadata value updates

Here’s the script we’ll be discussing:

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 UpdateItemMetadata($siteurl, $listname, $itemid, $createdbyUser, $modifedByUser, $createdDate, $modifiedDate)
        $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)
        $eModifedByUser = $ctx.Web.EnsureUser($modifedByUser)
        $item = $list.GetItemById($itemid) 
        #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

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

This script starts by loading the SharePoint Client assemblies which are required to interact with SharePoint Online. It then defines a function UpdateItemMetadata that takes several parameters including the SharePoint site URL, list name, item ID, user details for the “CreatedBy” and “ModifiedBy” fields, and timestamps for the “Created” and “Modified” fields.

Inside the function, it first prompts for credentials using Get-Credential and creates a SharePoint Online credentials object. It then creates a ClientContext object for the SharePoint site. The EnsureUser method is used to get the user objects for the “CreatedBy” and “ModifiedBy” fields. This method checks if a user exists within the site collection.

It fetches the list and the specific item in the list using the provided list name and item ID. The “Author” and “Editor” fields of the item (which correspond to “CreatedBy” and “ModifiedBy”) are updated with the user objects. The “Created” and “Modified” fields of the item are updated with the provided timestamps.

The item is updated using the Update method and the changes are submitted to the server using ExecuteQuery. If the update is successful, a success message is printed. If an error occurs, the error message is printed.

Function Call

Finally, the function is called with specific parameters to update a particular item in a SharePoint list.

UpdateItemMetadata -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"

By using this script, administrators can ensure that all specified fields are updated with the correct settings. This is particularly useful in large organizations where there may be a need to update multiple fields across various lists.

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