Skip to content
Rishan Solutions
Rishan Solutions
  • PowerApps
  • SharePoint online
    • Uncategorized
    • Uncategorized
  • PowerAutomate
Rishan Solutions
Latest Posts
  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025 June 24, 2025
  • Recursive Queries in T-SQL May 7, 2025
  • Generating Test Data with CROSS JOIN May 7, 2025
  • Working with Hierarchical Data May 7, 2025
  • Using TRY_CAST vs CAST May 7, 2025
  • Dynamic SQL Execution with sp_executesql May 7, 2025

Bulk Migrating Documents to SharePoint Online using PnP PowerShell

Posted on March 18, 2025March 18, 2025 by Rishan Solutions

Loading

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!

Posted Under PNP PowerShellBulk Upload Document Management File Transfer Metadata Retention PNP PowerShell SharePoint Automation SharePoint Online Migration

Post navigation

IoT and Blockchain for Supply Chain Management
IoT for Smart Grid and Energy Distribution

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Agentic AI: The Dawn of Autonomous Intelligence Revolutionizing 2025
  • Recursive Queries in T-SQL
  • Generating Test Data with CROSS JOIN
  • Working with Hierarchical Data
  • Using TRY_CAST vs CAST

Recent Comments

  1. Michael Francis on Search , Filter and Lookup in power apps
  2. A WordPress Commenter on Hello world!

Archives

  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • March 2024
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • January 2022

Categories

  • Active Directory
  • AI
  • AngularJS
  • Blockchain
  • Button
  • Buttons
  • Choice Column
  • Cloud
  • Cloud Computing
  • Data Science
  • Distribution List
  • DotNet
  • Dynamics365
  • Excel Desktop
  • Extended Reality (XR) – AR, VR, MR
  • Gallery
  • Icons
  • IoT
  • Java
  • Java Script
  • jQuery
  • Microsoft Teams
  • ML
  • MS Excel
  • MS Office 365
  • MS Word
  • Office 365
  • Outlook
  • PDF File
  • PNP PowerShell
  • Power BI
  • Power Pages
  • Power Platform
  • Power Virtual Agent
  • PowerApps
  • PowerAutomate
  • PowerPoint Desktop
  • PVA
  • Python
  • Quantum Computing
  • Radio button
  • ReactJS
  • Security Groups
  • SharePoint Document library
  • SharePoint online
  • SharePoint onpremise
  • SQL
  • SQL Server
  • Template
  • Uncategorized
  • Variable
  • Visio
  • Visual Studio code
  • Windows
© Rishan Solutions 2025 | Designed by PixaHive.com.
  • Rishan Solutions