SharePoint Online: Get File Properties using PowerShell

Requirement:  Get File Properties in SharePoint Online using PowerShell.

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.

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “SharePoint Online: Get File Properties using PowerShell

  • Nevermind I found the answer. Had to convert the string to int

    Reply
  • Is there a way to filter the files based on a specific property value? Eg. Year is less than 2020. Tried the following it’s not working:
    Where-Object method: Get-PnPFile -Url $FileRelativeURL -AsListItem | Where-object {_.item[“Year”] -lt ‘2020’}

    If method: $Item = Get-PnPFile -Url $FileRelativeURL -AsListItem
    If ($Item[“Year”] -lt ‘2020’)
    {Write-Host “Plan year: “$Item[“Year”] $FileRelativeURL}

    Reply
  • Great script! Is there a way to obtain the created and accessed dates of the file with the PnP PowerShell

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *