Downloading files from a SharePoint Online Document Library using PnP PowerShell is useful for backup, migration, automation, and synchronization tasks. With PnP PowerShell, you can download single files, multiple files, or entire folders efficiently.
What You’ll Learn:
✔️ How to connect to SharePoint Online
✔️ How to download a single file
✔️ How to download multiple files
✔️ How to download all files in a folder
✔️ How to handle errors and automate downloads
Prerequisites
Before proceeding, ensure that:
PnP PowerShell is installed
You have SharePoint Online permissions (Read or higher)
You have the SharePoint site URL
You know the library and folder structure
Step 1: Install and Import PnP PowerShell
If you haven’t installed PnP PowerShell, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
Then, import the module:
Import-Module PnP.PowerShell
PnP PowerShell is ready!
Step 2: Connect to SharePoint Online
Before downloading files, connect to your SharePoint site:
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
🔹 Replace "yourtenant"
with your tenant name
🔹 Replace "yoursite"
with the actual site name
Connected successfully!
Step 3: Download a Single File from a Document Library
To download a single file, specify the document library path and local save location:
# Define variables
$libraryPath = "/sites/yoursite/Shared Documents/report.pdf"
$localPath = "C:\Users\YourName\Downloads\report.pdf"
# Download the file
Get-PnPFile -ServerRelativeUrl $libraryPath -Path $localPath -AsFile -Force
Write-Host "File downloaded successfully to $localPath"
Replace:
"Shared Documents"
with your document library"report.pdf"
with your file name"C:\Users\YourName\Downloads\"
with your local folder path
Single file downloaded successfully!
Step 4: Download Multiple Files from a Document Library
To download all files in a library, use the following script:
# Define variables
$libraryName = "Shared Documents"
$localDownloadPath = "C:\Users\YourName\Downloads\LibraryBackup"
# Create local folder if not exists
if (!(Test-Path -Path $localDownloadPath)) {
New-Item -ItemType Directory -Path $localDownloadPath
}
# Get all files in the library
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FileSystemObjectType -eq "File" }
# Download each file
foreach ($file in $files) {
$fileUrl = $file.FieldValues.FileRef
$fileName = $file.FieldValues.FileLeafRef
Get-PnPFile -ServerRelativeUrl $fileUrl -Path "$localDownloadPath\$fileName" -AsFile -Force
Write-Host "Downloaded: $fileName"
}
🔹 This script:
✔️ Gets all files from the library
✔️ Downloads them to a local folder
Multiple files downloaded successfully!
Step 5: Download All Files from a Folder
If your files are inside a specific folder in the document library, use this script:
# Define variables
$libraryName = "Shared Documents"
$folderPath = "/sites/yoursite/Shared Documents/Reports"
$localDownloadPath = "C:\Users\YourName\Downloads\ReportsBackup"
# Create local folder if not exists
if (!(Test-Path -Path $localDownloadPath)) {
New-Item -ItemType Directory -Path $localDownloadPath
}
# Get all files in the folder
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FieldValues.FileRef -like "$folderPath/*" }
# Download each file
foreach ($file in $files) {
$fileUrl = $file.FieldValues.FileRef
$fileName = $file.FieldValues.FileLeafRef
Get-PnPFile -ServerRelativeUrl $fileUrl -Path "$localDownloadPath\$fileName" -AsFile -Force
Write-Host "Downloaded: $fileName"
}
Replace:
"Reports"
with your folder name
All files from the folder downloaded successfully!
Step 6: Check If a File Exists Before Downloading
To prevent re-downloading existing files, modify the script:
# Define variables
$fileUrl = "/sites/yoursite/Shared Documents/report.pdf"
$localPath = "C:\Users\YourName\Downloads\report.pdf"
# Check if file exists
if (Test-Path -Path $localPath) {
Write-Host "File already exists. Skipping download."
} else {
Get-PnPFile -ServerRelativeUrl $fileUrl -Path $localPath -AsFile -Force
Write-Host "File downloaded successfully."
}
Duplicate check implemented!
Step 7: Automate Daily File Downloads
If you need to download files daily, create a PowerShell Task Scheduler:
1️⃣ Save the script as DownloadFiles.ps1
2️⃣ Open Task Scheduler
3️⃣ Create a new task
4️⃣ Set Trigger: Daily at a specific time
5️⃣ Set Action: Run PowerShell script
Automated daily downloads configured!
Step 8: Delete Downloaded Files from SharePoint (Optional)
If you want to move files instead of copying, delete them after downloading:
# Delete file after downloading
Remove-PnPFile -ServerRelativeUrl "/sites/yoursite/Shared Documents/report.pdf" -Force
Write-Host "File deleted from SharePoint after download."
Downloaded files removed from SharePoint!
Common Errors & Solutions
Error | Cause | Solution |
---|---|---|
Access Denied | Insufficient permissions | Ensure you have Read or Contribute rights |
Cannot find library | Incorrect library name | Use Get-PnPList to verify the name |
File not found | Wrong path or missing file | Use Get-PnPListItem to check the file location |