SharePoint Online: How to Update Document Metadata using PowerShell?
Requirement: Update Metadata of a File in SharePoint Online using PowerShell
How to bulk edit metadata in SharePoint Online?
If you want to change properties of multiple documents in a SharePoint Online document library at once, you can use the details pane in Modern experience to bulk edit file metadata.
Follow these steps to bulk update document properties:
- Browse to your SharePoint Online document library.
- Select the documents from the document library.
- Click on the little “i” button to open details pane.
- Now, You can change the properties of the selected documents in bulk.
- Click Save to apply the changes.
SharePoint Online: PowerShell to Update Metadata of a File
Let me show you the PowerShell to update a file’s metadata in SharePoint Online. This can be helpful if you need to make updates to a large number of files at once or if you want to automate the process.
#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 Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$FileRelativeURL="/sites/Marketing/Shared Documents/Release Notes.dotx"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#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 File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.ExecuteQuery()
#Set metadata of the File
$ListItem = $File.ListItemAllFields
$ListItem["ReviewTimestamp"] = [System.DateTime]::Now
$ListItem.Update()
$Ctx.ExecuteQuery()
write-host -f Green "Document Metadata Updated Successfully!"
}
Catch {
write-host -f Red "Error: " $_.Exception.Message
}
PowerShell to Set Metadata Value for All Files in a SharePoint Online Document Library
How about updating properties for all files in a 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"
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$ListName="Documents"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get All Items from the List
$List = $Ctx.web.Lists.GetByTitle($ListName)
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Loop through each item
ForEach($ListItem in $ListItems)
{
#Set metadata of the List Item
$ListItem["ReviewTimestamp"] = [System.DateTime]::Now
$ListItem.Update()
write-host -f Green "Document Metadata Updated Successfully for:" $ListItem["FileLeafRef"]
}
$Ctx.ExecuteQuery()
}
Catch {
write-host -f Red "Error: " $_.Exception.Message
}
PnP PowerShell to Set Metadata of a File
Here is how you can upload a file to the SharePoint Online document library and set its metadata using PnP PowerShell:
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/branding"
$FilesPath = "C:\Temp\Technical Design.docx"
$SiteRelativePath = "/documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the File from the local path
$File = Get-ChildItem -Path $FilesPath
#Get Created on, Last Modified Properties of the file
$Created = [DateTime]$File.CreationTime
$Modified = [DateTime]$File.LastWriteTime
#Upload the file to SharePoint Online folder - and Set Metadata
Add-PnPFile -Path $File.FullName -Folder $SiteRelativePath -Values @{"Title" = $($File.Name);"Created"=$Created;"Modified"=$Modified}
Similarly, to set metadata of an existing file, use:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$ListName = "Branding"
$SiteRelativeURL = "/Branding/Technical Design.docx"
$CreatedBy= "[email protected]"
$Title = "New Design Document V2.docx"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the File from SharePoint
$File = Get-PnPFile -Url $SiteRelativeURL -AsListItem
#Update document properties
Set-PnPListItem -List $ListName -Identity $File.Id -Values @{"Title" = $Title; "Author" = $CreatedBy}
If you want to bulk edit document properties from a CSV file, refer: SharePoint Online: Bulk Update Metadata from a CSV File using PowerShell
How to update metadata of a folder in a document library using pnp PowerShell?