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

Migrating File Shares to SharePoint Online using PnP PowerShell

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

Loading

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:

  1. Save the script as FileShareMigration.ps1.
  2. Open Task Scheduler → Create Basic Task.
  3. Set Trigger → Daily, Weekly, or Custom Interval.
  4. Set Action → Start a Program.
  5. 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.

Posted Under PNP PowerShellAutomation Cloud Storage File Migration File Shares Metadata Preservation PNP PowerShell SharePoint online Task Scheduling

Post navigation

Java LDAP Authentication and Integration
Creating a SharePoint Online Backup Strategy using PnP PowerShell

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