Collaborate, Innovate, Automate

Set List Item by Metadata

This PnP PowerShell script reads a CSV file and updates SharePoint Online list items by matching a metadata value — such as a site name stored in the Title field — rather than requiring the numeric item ID. It looks up each matching item with a CAML query, then updates one or more fields per row.

Purpose

This script helps with SharePoint list management by:

Prerequisites

CSV Format

The CSV file should include a Site column matching the Title field of the target list item, plus one column per field being updated. Example:

Site,Column1,Column2,Column3
https://tenantName.sharepoint.com/sites/siteName,Valuse1,Value11,Value111

PowerShell Script

$SiteUrl = 'https://SiteName.sharepoint.com/sites/SiteName'
$ClientId = ""
$ListName = "<listName>"
if ($null -eq $SiteUrl) { exit; }

Connect-PnPOnline -Url $SiteURL -Interactive -ClientId $ClientId
Write-Host [Success] Connected to $SiteUrl -ForegroundColor Green

$csvfile = "ComplexUpdate.csv"
$sites = Import-Csv $csvfile

$mydatestring=get-date -format yyyyMMddHHmm
$Statusreport= "Report-ComplexUpdates-$mydatestring.csv"
"Site,Column1,Column2,Column3" | add-content $Statusreport


#=========================================================
#1. Get URLS from CSV Report & update Current Value
#=========================================================


Foreach ($site in $sites)
    {

        #================= query list item by Title======================

        $getCurrentStatus=$null

        $CurrentStatus="<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$($site.'Site')</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>"

        $getCurrentStatus=Get-PnPListItem -List $ListName -Query $CurrentStatus -ErrorAction SilentlyContinue

        #==================Update fields===================

        Set-PnPListItem -List $ListName -Identity $getCurrentStatus -Values @{"Column1" = "$($site.'Column1')";"Column2" = "$($site.'Column2')";"Column3" = "$($site.'Column3')"}

        #================= update list item END======================

    }


write-host "Completed"
          

Usage Notes

Example CSV format for Set List Item by Metadata script