SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM

No comments

In this post, I will show that how to update SharePoint online document library column or metadata value in a batch using PowerShell CSOM code

This is my  SharePoint online document library which has “DocumentStatus” column with the various status where I will change the document status to Closed if the document is approved.


PowerShell Script:

#Load SharePoint CSOM Assemblies
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"

$UserName = ""
$Password = "ThisIsTestPassword"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
$BatchSize =1000
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$emptyString = ""
#Define CAML Query to get Files from the list in batches
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
$count =0

#Get List Items in Batches
    $ListItems = $List.GetItems($Query)

    #Update Postion of the ListItemCollectionPosition
    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
    If ($ListItems.Count -eq 0) { Break }
    #Update List Item
    ForEach($Item in $ListItems)
        #Update List Item Title as File Name


        If($documentStatus -eq "Approved")


            #Get the List Item with all metadata fields of the File
            #$ListItem = $file.ListItemAllFields


    Write-Host "============================================================="
    Write-Host $count
    Write-Host "============================================================="
}While ($Query.ListItemCollectionPosition -ne $null)

After executing the above script we can see whichever items had with “Approved” status now changed to “Closed” status.