Exporting SharePoint document libraries to local storage is useful for backup, migration, or offline access. PnP PowerShell provides an efficient way to download entire libraries while maintaining folder structures and metadata.
1. Introduction
A SharePoint document library is a collection of files stored within a SharePoint Online site. There are several scenarios where you may want to export its contents, such as:
Creating a backup of critical documents
Migrating files to another SharePoint site or local drive
Archiving old documents for compliance
Making files accessible offline
With PnP PowerShell, this process is automated, ensuring accurate file retrieval while maintaining folder structures and metadata.
2. Prerequisites
Before starting, ensure:
- You have PnP PowerShell installed. If not, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force
- You have SharePoint Site Owner or Administrator permissions.
- You have the URL of the SharePoint site and document library.
3. Connecting to SharePoint Online
Before exporting, authenticate to SharePoint.
Step 1: Connect to SharePoint
$SiteURL = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteURL -Interactive
- Replace
"yourtenant"
with your SharePoint tenant name. - Replace
"YourSite"
with your actual SharePoint site name.
This command prompts a login window for authentication.
4. Retrieving SharePoint Library Content
Now, fetch files and folders from the document library.
Step 1: Get Library Items
$LibraryName = "Documents"
$Files = Get-PnPListItem -List $LibraryName -Fields FileRef, FileLeafRef -PageSize 5000
- Replace
"Documents"
with your library name. - PageSize 5000 ensures large libraries load efficiently.
This command retrieves all files and folders in the Documents library.
5. Exporting Files to Local Storage
Create a backup folder and download all files.
Step 1: Define Local Backup Path
$LocalBackupPath = "C:\SharePointBackup"
if (!(Test-Path -Path $LocalBackupPath)) { New-Item -ItemType Directory -Path $LocalBackupPath }
This creates a backup folder if it does not exist.
Step 2: Download Files
foreach ($File in $Files) {
$FileUrl = $File["FileRef"]
$FileName = $File["FileLeafRef"]
$LocalPath = Join-Path -Path $LocalBackupPath -ChildPath $FileName
Write-Host "Downloading: $FileName"
Get-PnPFile -ServerRelativeUrl $FileUrl -Path $LocalBackupPath -AsFile -Force
}
- This downloads each file into the local backup folder.
-Force
ensures files overwrite existing copies if needed.
6. Preserving Folder Structure
To maintain the original folder hierarchy, we need to recreate the structure locally before downloading files.
Step 1: Extract Folder Structure
$Folders = Get-PnPListItem -List $LibraryName | Where-Object { $_["FileRef"] -match "/Forms/" -eq $false }
This fetches folders while ignoring system folders like Forms.
Step 2: Recreate Folder Structure Locally
foreach ($Folder in $Folders) {
$FolderPath = $Folder["FileRef"] -replace "/sites/YourSite/$LibraryName", ""
$LocalFolderPath = Join-Path -Path $LocalBackupPath -ChildPath $FolderPath
if (!(Test-Path -Path $LocalFolderPath)) { New-Item -ItemType Directory -Path $LocalFolderPath }
}
- This script extracts the relative folder path and recreates it locally.
- Ensure the folder path mapping is correct before running.
Step 3: Download Files to Respective Folders
foreach ($File in $Files) {
$FileUrl = $File["FileRef"]
$FileName = $File["FileLeafRef"]
$RelativeFolderPath = ($FileUrl -replace "/sites/YourSite/$LibraryName/", "") -replace "/$FileName", ""
$LocalFolderPath = Join-Path -Path $LocalBackupPath -ChildPath $RelativeFolderPath
if (!(Test-Path -Path $LocalFolderPath)) { New-Item -ItemType Directory -Path $LocalFolderPath }
$LocalFilePath = Join-Path -Path $LocalFolderPath -ChildPath $FileName
Get-PnPFile -ServerRelativeUrl $FileUrl -Path $LocalFilePath -AsFile -Force
}
- This ensures each file is downloaded into its corresponding folder.
7. Handling Metadata
Metadata like Created Date, Modified Date, and Author can be exported to a CSV file.
Step 1: Export Metadata to CSV
$MetadataPath = "C:\SharePointBackup\Metadata.csv"
$FileMetadata = @()
foreach ($File in $Files) {
$FileMetadata += [PSCustomObject]@{
FileName = $File["FileLeafRef"]
FilePath = $File["FileRef"]
Created = $File["Created"]
Modified = $File["Modified"]
Author = $File["Author"]
}
}
$FileMetadata | Export-Csv -Path $MetadataPath -NoTypeInformation
- This exports file details into a CSV file for reference.
8. Automating the Export Process
To schedule regular backups, save the script as Export-SharePointLibrary.ps1
and automate it using Task Scheduler.
Step 1: Schedule in Task Scheduler
- Open Task Scheduler.
- Click Create Basic Task.
- Choose a Trigger (daily, weekly, or monthly).
- Choose Action > Start a Program.
- Select powershell.exe and pass the script path as an argument.
- Save and enable the task.
This ensures regular backups without manual intervention.