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 Moving SharePoint Online Files Between Libraries using PnP PowerShell

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

Loading

Managing files in SharePoint Online often requires moving large numbers of files between document libraries to reorganize content, optimize storage, or comply with governance policies. PnP PowerShell provides a seamless way to automate this process efficiently.


Key Considerations Before Moving Files

✔ Source and Destination Libraries – Ensure both libraries exist in the same site.
✔ Permissions – The user must have edit permissions on both libraries.
✔ Metadata Retention – Ensure file metadata is preserved after the move.
✔ Version History – Moving files does not retain version history; consider copying if needed.


Step 1: Connect to SharePoint Online

First, connect to your SharePoint Online site using PnP PowerShell.

$siteUrl = "https://yourtenant.sharepoint.com/sites/YourSite"

# Connect to SharePoint Online
Connect-PnPOnline -Url $siteUrl -Interactive

✔ Ensures authentication to perform file operations.


Step 2: Define Source and Destination Libraries

Specify the source and destination document libraries where files will be moved.

$sourceLibrary = "SourceDocuments"
$destinationLibrary = "DestinationDocuments"

✔ Adjust these names as per your SharePoint site.


Step 3: Retrieve and Move Files in Bulk

Use PnP PowerShell to fetch all files and move them to the destination.

# Get all files from the source library
$files = Get-PnPListItem -List $sourceLibrary -PageSize 2000

foreach ($file in $files) {
$fileUrl = $file.FieldValues["FileRef"] # Get the full file path
$fileName = $file.FieldValues["FileLeafRef"] # Get the file name
$destinationPath = "/sites/YourSite/$destinationLibrary/$fileName"

# Move the file
Move-PnPFile -ServerRelativeUrl $fileUrl -TargetUrl $destinationPath -Force
Write-Host "✅ Moved: $fileName"
}

✔ Moves all files while maintaining the folder structure.


Step 4: Handling Folder Structure

To move files along with folders, include a folder check before moving.

# Get all items (files and folders)
$items = Get-PnPListItem -List $sourceLibrary -PageSize 2000

foreach ($item in $items) {
$fileRef = $item.FieldValues["FileRef"]
$fileLeaf = $item.FieldValues["FileLeafRef"]

# Determine if the item is a folder
if ($item.FieldValues["FSObjType"] -eq 1) {
# Create the folder in the destination if it doesn’t exist
$folderPath = "/sites/YourSite/$destinationLibrary/$fileLeaf"
New-PnPFolder -Name $fileLeaf -List $destinationLibrary
Write-Host " Created Folder: $fileLeaf"
} else {
# Move the file while maintaining folder structure
$destinationPath = "/sites/YourSite/$destinationLibrary/$fileLeaf"
Move-PnPFile -ServerRelativeUrl $fileRef -TargetUrl $destinationPath -Force
Write-Host " Moved File: $fileLeaf"
}
}

✔ Ensures both files and folders are moved correctly.


Step 5: Preserve Metadata During Move

Since moving files removes modified dates, use a custom script to retain metadata.

foreach ($file in $files) {
$fileUrl = $file.FieldValues["FileRef"]
$fileName = $file.FieldValues["FileLeafRef"]
$modifiedBy = $file.FieldValues["Editor"].Email
$modifiedDate = $file.FieldValues["Modified"]

$destinationPath = "/sites/YourSite/$destinationLibrary/$fileName"

# Move the file
Move-PnPFile -ServerRelativeUrl $fileUrl -TargetUrl $destinationPath -Force

# Restore metadata
Set-PnPListItem -List $destinationLibrary -Identity $file.Id -Values @{
"Editor" = $modifiedBy
"Modified" = $modifiedDate
}
Write-Host " Moved and Restored Metadata: $fileName"
}

✔ Ensures last modified user and timestamp remain intact.


Step 6: Generate a Move Report

Create a CSV report to log file movement for audit purposes.

$report = @()
foreach ($file in $files) {
$fileUrl = $file.FieldValues["FileRef"]
$fileName = $file.FieldValues["FileLeafRef"]
$destinationPath = "/sites/YourSite/$destinationLibrary/$fileName"

# Log each file move
$report += [PSCustomObject]@{
FileName = $fileName
SourcePath = $fileUrl
DestinationPath = $destinationPath
Status = "Moved"
}
}

# Export report
$report | Export-Csv -Path "C:\SharePointFileMoveReport.csv" -NoTypeInformation
Write-Host "📄 Report Generated: C:\SharePointFileMoveReport.csv"

✔ Provides a detailed log of moved files.


Step 7: Delete Empty Folders in Source Library

Once files are moved, delete empty folders to clean up the source library.

$folders = Get-PnPListItem -List $sourceLibrary | Where-Object { $_.FieldValues["FSObjType"] -eq 1 }

foreach ($folder in $folders) {
$folderUrl = $folder.FieldValues["FileRef"]
Remove-PnPListItem -List $sourceLibrary -Identity $folder.Id -Recycle
Write-Host "🗑 Deleted Empty Folder: $folderUrl"
}

✔ Keeps your document library organized.

Posted Under PNP PowerShellBulk File Move Document Libraries File Migration Metadata Retention Microsoft 365 PNP PowerShell SharePoint online

Post navigation

Java Serverless Functions on Google Cloud Functions
Java Application Deployment with Helm Charts

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