Metadata in SharePoint Online provides essential details about documents, such as title, author, created date, modified date, version, and custom columns. Exporting this metadata can be useful for auditing, compliance, and reporting.
This guide covers how to export document metadata from SharePoint Online using PnP PowerShell, including:
✔ Connecting to SharePoint Online
✔ Exporting metadata from a document library
✔ Filtering metadata based on conditions
✔ Saving metadata to CSV for reporting
Step 1: Install and Connect to SharePoint Online
Ensure you have PnP PowerShell installed:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
Update the module if needed:
Update-Module -Name PnP.PowerShell
Now, connect to SharePoint Online:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive
Step 2: Export Document Metadata from a Library
To export all document metadata from a specific document library, run:
# Define variables
$libraryName = "Documents" # Change this to your document library name
$exportFile = "C:\Backups\DocumentMetadata.csv"
# Get all items in the library
$items = Get-PnPListItem -List $libraryName -Fields "FileLeafRef", "FileRef", "Author", "Editor", "Created", "Modified"
# Create an array to store metadata
$metadataList = @()
foreach ($item in $items) {
$metadataList += [PSCustomObject]@{
"File Name" = $item["FileLeafRef"]
"File URL" = $item["FileRef"]
"Created By" = $item["Author"].Email
"Modified By" = $item["Editor"].Email
"Created Date" = $item["Created"]
"Modified Date" = $item["Modified"]
}
}
# Export to CSV
$metadataList | Export-Csv -Path $exportFile -NoTypeInformation
Write-Host "Metadata exported to $exportFile"
✔ Saves file name, URL, author, editor, and timestamps to a CSV file.
Step 3: Export Custom Metadata Fields
If your document library has custom columns (e.g., Category, Department), include them:
$customFields = "Category", "Department" # Change to match your metadata fields
$items = Get-PnPListItem -List $libraryName -Fields "FileLeafRef", "FileRef", "Author", "Editor", "Created", "Modified", $customFields
$metadataList = @()
foreach ($item in $items) {
$metadataList += [PSCustomObject]@{
"File Name" = $item["FileLeafRef"]
"File URL" = $item["FileRef"]
"Created By" = $item["Author"].Email
"Modified By" = $item["Editor"].Email
"Created Date" = $item["Created"]
"Modified Date" = $item["Modified"]
"Category" = $item["Category"]
"Department" = $item["Department"]
}
}
$metadataList | Export-Csv -Path $exportFile -NoTypeInformation
Write-Host "Metadata including custom fields exported to $exportFile"
✔ Captures custom metadata fields along with standard ones.
Step 4: Filter Metadata Based on Conditions
To export metadata only for files modified in the last 30 days:
$days = 30
$cutoffDate = (Get-Date).AddDays(-$days)
$filteredItems = $items | Where-Object { $_["Modified"] -gt $cutoffDate }
$filteredItems | Export-Csv -Path "C:\Backups\RecentFiles.csv" -NoTypeInformation
Write-Host "Exported only files modified in the last $days days."
✔ Helps track recently updated files.
Step 5: Automate Metadata Export with Task Scheduler
To schedule daily metadata exports:
- Save the script as
ExportMetadata.ps1
. - Open Task Scheduler → Create Basic Task.
- Set Trigger → Daily at 1:00 AM.
- Set Action → Start a Program.
- Use this PowerShell command:
-ExecutionPolicy Bypass -File "C:\Scripts\ExportMetadata.ps1"
✔ Ensures automated daily exports without manual effort.