![]()
Organizations often store their files in network file shares, but SharePoint Online offers a more secure, accessible, and collaborative solution. Migrating file shares to SharePoint Online using PnP PowerShell allows for:
✔ Efficient bulk file migration
✔ Automation to save time
✔ Retention of metadata (e.g., modified dates, owners)
This guide walks you through the step-by-step process of migrating file shares to SharePoint Online using PnP PowerShell.
Step 1: Install and Connect to SharePoint Online
Ensure PnP PowerShell is installed:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
Update-Module -Name PnP.PowerShell
Connect to SharePoint Online:
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/YourSite" -Interactive
Step 2: Define Source and Destination Paths
Specify the file share path and target SharePoint library:
$sourcePath = "\\NetworkShare\Files"
$destinationLibrary = "Shared Documents"
Step 3: Retrieve Files from the File Share
Fetch files from the source directory:
$files = Get-ChildItem -Path $sourcePath -Recurse -File
Write-Host "Total files found: $($files.Count)"
✔ Ensures all files are identified for migration.
Step 4: Upload Files to SharePoint Online
Loop through files and upload them to SharePoint Online:
foreach ($file in $files) {
$relativePath = $file.FullName.Replace($sourcePath, "").TrimStart("\")
Add-PnPFile -Path $file.FullName -Folder "$destinationLibrary/$relativePath"
Write-Host "Uploaded: $($file.FullName)"
}
Write-Host "File migration completed successfully."
✔ Migrates all files while maintaining folder structure.
Step 5: Preserve Metadata (Modified & Created Dates)
By default, SharePoint assigns new timestamps to uploaded files. To retain original metadata:
foreach ($file in $files) {
$relativePath = $file.FullName.Replace($sourcePath, "").TrimStart("\")
# Upload file
$uploadedFile = Add-PnPFile -Path $file.FullName -Folder "$destinationLibrary/$relativePath"
# Update metadata
Set-PnPListItem -List $destinationLibrary -Identity $uploadedFile.ListItemAllFields.Id -Values @{
"Modified" = $file.LastWriteTime;
"Created" = $file.CreationTime
}
Write-Host "Uploaded and metadata updated: $($file.FullName)"
}
Write-Host "File migration with metadata preservation completed."
✔ Ensures original timestamps remain intact in SharePoint.
Step 6: Automate Migration with Task Scheduler
To run the script automatically:
- Save the script as
FileShareMigration.ps1. - Open Task Scheduler → Create Basic Task.
- Set Trigger → Daily, Weekly, or Custom Interval.
- Set Action → Start a Program.
- Use this PowerShell execution command:
-ExecutionPolicy Bypass -File "C:\Scripts\FileShareMigration.ps1"
✔ Automates file share migration at regular intervals.
Step 7: Verify Migration
To confirm successful migration:
Get-PnPListItem -List "Shared Documents" | Select-Object Title, FileRef, Modified, Created
✔ Displays uploaded file details in SharePoint.
