Migrating a large number of documents to SharePoint Online can be automated using PnP PowerShell. This guide will help you bulk upload files, retain metadata, and ensure a smooth transition.
What You’ll Learn
Connecting to SharePoint Online
Bulk uploading files to a Document Library
Retaining metadata (Created Date, Modified Date, Owner)
Handling large file migrations efficiently
Step 1: Install & Connect to SharePoint Online
Ensure PnP PowerShell is installed:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force
Connect to your SharePoint site:
$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
Connect-PnPOnline -Url $SiteUrl -Interactive
Successfully connected!
Step 2: Bulk Upload Files to a Document Library
This script uploads all files from a local folder to a SharePoint Document Library.
Save this script as C:\Migration\BulkUploadFiles.ps1
# Define Variables
$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$LibraryName = "Shared Documents"
$LocalFolderPath = "C:\Migration\Files"
# Connect to SharePoint
Connect-PnPOnline -Url $SiteUrl -Interactive
# Get all files from the folder
$Files = Get-ChildItem -Path $LocalFolderPath -File
# Loop through files and upload them
foreach ($File in $Files) {
$FilePath = $File.FullName
$TargetFolder = "/$LibraryName/" # SharePoint Library Path
Write-Host "Uploading: $($File.Name) ..."
Add-PnPFile -Path $FilePath -Folder $TargetFolder -Connection (Get-PnPConnection)
Write-Host "Uploaded: $($File.Name) "
}
Write-Host "All files uploaded successfully!"
Files are now uploaded to SharePoint!
Step 3: Retain Metadata (Created Date, Modified Date, Owner)
By default, SharePoint changes the file Created Date, Modified Date, and Owner to the upload date.
This script preserves metadata while uploading.
Save this script as C:\Migration\UploadWithMetadata.ps1
# Define Variables
$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$LibraryName = "Shared Documents"
$LocalFolderPath = "C:\Migration\Files"
# Connect to SharePoint
Connect-PnPOnline -Url $SiteUrl -Interactive
# Get all files
$Files = Get-ChildItem -Path $LocalFolderPath -File
# Loop through files
foreach ($File in $Files) {
$FilePath = $File.FullName
$TargetFolder = "/$LibraryName/"
# Get original metadata
$Created = $File.CreationTime
$Modified = $File.LastWriteTime
$Owner = (Get-Acl $FilePath).Owner
Write-Host "Uploading: $($File.Name) ..."
# Upload file
$UploadedFile = Add-PnPFile -Path $FilePath -Folder $TargetFolder
# Set Metadata
Set-PnPListItem -List $LibraryName -Identity $UploadedFile.ListItemAllFields["ID"] -Values @{"Created" = $Created; "Modified" = $Modified; "Author" = $Owner}
Write-Host "Uploaded with metadata: $($File.Name) ✅"
}
Write-Host "All files uploaded with metadata successfully!"
Metadata is now retained in SharePoint!
Step 4: Handling Large File Migrations
For large migrations, break files into batches to avoid errors.
Use this modified script for large file uploads:
# Define Variables
$SiteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"
$LibraryName = "Shared Documents"
$LocalFolderPath = "C:\Migration\Files"
$BatchSize = 10 # Upload 10 files at a time
# Connect to SharePoint
Connect-PnPOnline -Url $SiteUrl -Interactive
# Get all files
$Files = Get-ChildItem -Path $LocalFolderPath -File
$TotalFiles = $Files.Count
$UploadedCount = 0
# Process files in batches
foreach ($Batch in ($Files | Group-Object -Property { [math]::Floor([array]::IndexOf($Files, $_) / $BatchSize) })) {
foreach ($File in $Batch.Group) {
$FilePath = $File.FullName
$TargetFolder = "/$LibraryName/"
Write-Host "Uploading: $($File.Name) ..."
Add-PnPFile -Path $FilePath -Folder $TargetFolder -Connection (Get-PnPConnection)
$UploadedCount++
Write-Host "Uploaded: $($File.Name) ✅ [$UploadedCount/$TotalFiles]"
}
Write-Host "Pausing for 5 seconds to avoid throttling..."
Start-Sleep -Seconds 5
}
Write-Host "All files uploaded successfully!"
Prevents SharePoint throttling & speeds up migration!
Step 5: Verify Migration in SharePoint
After uploading, verify files:
Open Document Library → Check all files are uploaded.
Metadata Retention → Ensure Created/Modified dates match original files.
Check Permissions → Ensure correct users can access files.
Step 6: Automate & Schedule File Uploads
For daily uploads, use Task Scheduler:
1️⃣ Open Windows Task Scheduler
2️⃣ Create a New Task → Set it to run the script automatically.
3️⃣ Use PowerShell command:
powershell.exe -ExecutionPolicy Bypass -File "C:\Migration\BulkUploadFiles.ps1"
Now, files upload automatically!