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

In this blog post, we will take a look at how you can use PowerShell to get file properties in SharePoint Online. We’ll show you how to use PowerShell to get properties like the size, created date, path of a file, etc. 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://crescenttech.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://crescenttech.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 easily 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"]

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply