In this “SharePoint update metadata in the document library using PowerShell” post, I will show how to update SharePoint online document library metadata or column value in a batch using PowerShell CSOM code programmatically.
SharePoint update document library metadata using PowerShell CSOM
This is my SharePoint online document library which has the “DocumentStatus” column with the various status where I will change the document status to Closed if the document is approved.
Update document library metadata using PowerShell CSOM: SharePoint PowerShell update metadata
Use the below code to update document library metadata using PowerShell in SharePoint Online:
CLS #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" #Parameters $SiteURL="https://globalsharepoint.sharepoint.com/sites/TestSite/" $ListName="Documents" $UserName = "yourusername@globalsharepoint.onmicrosoft.com" $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) $Ctx.Load($List) $Ctx.ExecuteQuery() $emptyString = "" #Define CAML Query to get Files from the list in batches $Query = New-Object Microsoft.SharePoint.Client.CamlQuery $Query.ViewXml = "@ $BatchSize " $count =0 #Get List Items in Batches Do { $ListItems = $List.GetItems($Query) $Ctx.Load($ListItems) $Ctx.ExecuteQuery() $ListItems.Count #Update Postion of the ListItemCollectionPosition $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition $Query.ListItemCollectionPosition If ($ListItems.Count -eq 0) { Break } #Update List Item ForEach($Item in $ListItems) { #Update List Item Title as File Name $documentStatus=$Item["DocumentStatus"] If($documentStatus -eq "Approved") { $test=$Item; $file=$Item.File; #Get the List Item with all metadata fields of the File #$ListItem = $file.ListItemAllFields #$Ctx.Load($file) #$Ctx.ExecuteQuery() $Item["DocumentStatus"]="Closed" $Item.Update(); $Ctx.ExecuteQuery() } } Write-Host "=============================================================" Write-Host $count Write-Host "=============================================================" }While ($Query.ListItemCollectionPosition -ne $null)
After executing the above script we can see whichever items had “Approved” status now changed to “Closed” status.
Summary: SharePoint update document library metadata (update metadata SharePoint document library using PowerShell)
Thus, in this article, we have learned how to update document library metadata in SharePoint Online using PowerShell.
See Also: SharePoint Online PowerShell
You may also like the below SharePoint Online PowerShell tutorials:
- Get hub sites and associated sites using PnP PowerShell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- SharePoint Automation: PowerShell script to get remote server information
- SharePoint Online Automation – O365 – Send email from SharePoint online Using CSOM PowerShell script
- SharePoint Online Automation – O365 – Download files from a document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- Introduction to SharePoint information architecture
Buy SharePoint Online & Microsoft 365 Administration eBook

3 comments on “Safely SharePoint update metadata in document library using PowerShell CSOM – O365”