SharePoint Online: Update File Properties using PowerShell
Requirement: PowerShell to update metadata of a file in SharePoint Online.
PowerShell to Update the Properties of a File in SharePoint Online:
Updating document properties in Microsoft SharePoint Online can be a tedious task, especially if you need to update multiple properties for multiple documents. In this blog post, we’ll show you how to set metadata of a document in SharePoint Online using PowerShell.
Let’s update the “Status” Column value of a particular file in a SharePoint Online Document Library:
#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"
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops"
$FileRelativeUrl="/sites/Ops/Shared Documents/Investment Process.pptx"
Try {
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeUrl)
$Ctx.Load($File)
$Ctx.ExecuteQuery()
#Set Metadata of the File
$ListItem = $File.ListItemAllFields
$Listitem["Status"] = "Completed"
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "File's Metadata has been Updated Successfully!"
}
Catch {
write-host -f Red "Error Updating Metadata of the File!" $_.Exception.Message
}
and the result:
This PowerShell script sets the metadata column value of the given document. How to update the metadata property of all files in a document library?
PowerShell to Update Metadata Property of All Documents in a Library:
Let’s set a column value for all documents in the SharePoint Online document library using PowerShell.
#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://Crescent.sharepoint.com/sites/marketing"
$ListName="Branding"
$BatchSize = 2000
#Get Credentials to connect
$Cred= Get-Credential
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
#Define CAML Query to get Files from the list in batches
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = @"
<View Scope='RecursiveAll'>
<Query>
<OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>
</Query>
<RowLimit Paged='TRUE'>$BatchSize</RowLimit>
</View>
"@
$Counter = 1
#Get Files from the Library in Batches
Do {
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Update column value of the List Item
ForEach($Item in $ListItems)
{
#Fiter Files
If($Item.FileSystemObjectType -eq "File")
{
#Update List Item "Status" Column
$Item["Status"]="Completed"
$Item.Update()
$Ctx.ExecuteQuery()
}
Write-host "Updated Item $Counter of $($List.Itemcount)"
$Counter++
}
#Update Postion of the ListItemCollectionPosition
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
}While($Query.ListItemCollectionPosition -ne $null)
Here is another post on updating document metadata using PnP PowerShell, How to Update Document Metadata in SharePoint Online using PowerShell?
thanks for the script. How to I do this for a managed metedata column.
Here you go: How to Update Managed Metadata Column in SharePoint Online using PowerShell?
I did the same script but my column doesn’t appear in Sharepoint.
I try to did it on also on a folder (which is in fact a documentset type folder). The script pass without error but it doesn’t appear in SharePoint
Any idea ?
Hi I found a part of the problem. I added to my metadata a field named “test” which is in plain text. I’m able to update it using the script. However, the field that support multiple selection doesn’t work. Do I have to push it as an array ?
Exactly! Fields like single lines of text, choice works fine with this script. For other field types such as Date time, Person or group, Managed metadata, You have to handle them differently! E.g. To update Multiple choice column, use:
$ListItem[$FieldName] = @(“Development”, “IT Applications”, “Real Estate”)
Refer:
SharePoint Online: How to Update Choice Field Value using PowerShell?
Thanks for this post. How would we update the metadata of files in Library using a CSV file? ie. Update other fields in the library based on an item that is found in CSV file?
Sure, Here you go: SharePoint Online: Bulk Update Metadata Properties from a CSV File using PowerShell