SharePoint Online: Get File Properties using PowerShell
Requirement:Â Get File Properties in SharePoint Online using PowerShell.
SharePoint Online PowerShell to Get File Properties
Document properties are a set of metadata that provides additional information about a document, such as the author, date created, file type, and any custom column data. By using PowerShell, you can quickly retrieve document properties for one or multiple documents. In this article, we will explore how to use PowerShell to get document properties in SharePoint Online. Let’s get started!
#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"
Function Get-SPOFileProperties($SiteURL,$FileRelativeURL)
{
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
$Ctx.Load($File.Author)
$Ctx.Load($File.ModifiedBy)
$Ctx.Load($File.CheckedOutByUser)
$Ctx.Load($File.Author)
$Ctx.ExecuteQuery()
#Get File Properties
Write-host "File Name:" $File.Name
Write-host "Title:" $File.Title
Write-host "File Size:" $File.Length
Write-host "Created:" $File.TimeCreated
Write-host "Last Modified:" $File.TimeLastModified
Write-host "Created By:" $File.Author.Title
Write-host "Modified By:" $File.ModifiedBy.Title
Write-host "Checked Out To:" $File.CheckedOutByUser.Title
Write-host "UI Version:" $File.UIVersion
Write-host "Major Version:" $File.MajorVersion
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
}
#Call the Function with Site URL and File URL
Get-SPOFileProperties -SiteURL "https://Crescent.sharepoint.com" -FileRelativeURL "/Shared Documents/Discloser Asia.docx"
PowerShell to Get Document Metadata in SharePoint Online
If you need to retrieve custom column values 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"
$FileRelativeURL = "/Docs/Compliance Process.xlsx"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileRelativeURL)
$Ctx.Load($File)
#Get the List Item with all metadata fields of the File
$ListItem = $File.ListItemAllFields
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()
#Get Metadata of the File
Write-host "File Name:" $File.Name
#Get the "Status" column value of the File
Write-host "Status:" $ListItem["Status"]
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
PnP PowerShell to Get File Metadata column values
You’ll be able to quickly obtain the properties of your files within SharePoint Online using PnP PowerShell as:
#Parameter
$SiteURL= "https://crescent.sharepoint.com/sites/marketing/"
$FileRelativeURL = "/sites/marketing/Shared Documents/Proposal Template.docx"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the File as List Item
$File = Get-PnPFile -Url $FileRelativeURL -AsListItem
#To Get All Available Properties: $File.FieldValues
#Get File Metadata Properties - Field Values
Write-host "File Name:"$File["FileLeafRef"]
Write-host "Created By:"$File["Created_x0020_By"]
Write-host "Last Modified:"$File["Last_x0020_Modified"]
Write-host "File Size:"$File["File_x0020_Size"]
In summary, using PowerShell to get document properties in SharePoint Online can be helpful when you need to generate reports or audit large sets of documents, and use that information to organize, sort, or filter your documents.
Great script! Is there a way to obtain the created and accessed dates of the file with the PnP PowerShell
You can obtain the creation date of a site using: SharePoint Online: How to Get Site Creation Date using PowerShell?, But there are no properties to get the accessed date, AFAIK! (We have Last Activity as $web.LastItemUserModifiedDate, though!)
Created Date can be obtained from the File properties: PowerShell to Get File Properties (Such as: Created, Created By, etc.) in SharePoint Online To get download history, You can query Audit Logs from the Compliance center.