SharePoint Online: How to Update Document Metadata using PowerShell?
Requirement: Update Metadata of a File in SharePoint Online using PowerShell
SharePoint Online: PowerShell to Update Metadata of a File
PowerShell to Set Metadata Value for All Files in a SharePoint Online Document Library
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.
If you want to bulk edit document properties from a CSV file, refer: SharePoint Online: Bulk Update Metadata from a CSV File using PowerShell
SharePoint Online: PowerShell to Update Metadata of a File
#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
#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 -UseWebLogin #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 -UseWebLogin #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
No comments:
Please Login and comment to get your questions answered!