How do I use PnP to move large amounts of files without breaking sharing links and permissions?

Riley B 0 Reputation points
2025-01-27T20:10:17.82+00:00

I'm trying to find an archiving solution for my company that's easier than using the GUI and scrolling through large amounts of files and folders and manually using the Move To button. I have written a script to move files and folders from a users personal OneDrive site to a Sharepoint site that we use to archive ex-employee's data. The script creates a new folder on this Sharepoint site and then proceeds to move everything over.

The script works great except for one issue: all share links are broken and sharing permissions removed after the files/folders are moved.

Is there any way to emulate the functionality of the "Keep sharing with the same people" checkbox that you get in the OneDrive UI when moving files with PnP Powershell?

Code (I've removed identifying data with Xs):

$Domain = "XXX"
$User = Read-Host "Enter username of user being transferred (first part of email)" 

$UserSite = "https://$($Domain)-my.sharepoint.com/personal/$($User)_$($Domain)_com"
$UserSiteConnection = Connect-PnPOnline -Url $UserSite -Interactive -ClientID "XXXXXXXXXXXXXXXXXXXXXXX" -ReturnConnection

$DestinationSite = "https://$($Domain).sharepoint.com/sites/PrivateEmployeeArchive"
$DestinationSiteConnection = Connect-PnPOnline -Url $DestinationSite -Interactive -ClientID $UserSiteConnection.ClientId -ReturnConnection

#get all folders in base directory of user site
$UFolders = Get-PnPFolderInFolder -Identity "Documents" -Includes ServerRelativeUrl -Connection $UserSiteConnection

#get all files in base directory of user site
$UFiles = Get-PnPFileInFolder -Identity "Documents" -Includes ServerRelativePath -Connection $UserSiteConnection

#add new user folder to exEmployees "Archived OneDrive" folder, this is our destination location
Add-PnPFolder -Name "$User" -Folder "Shared Documents/Archived OneDrive" -Connection $DestinationSiteConnection

#move each folder (and contents) to archive folder
ForEach($folder in $UFolders){

    #get folder url and format it in Documents/<foldername> form
    $folderURL = "Documents$($folder.ServerRelativeUrl.split("Documents")[1])"

    Write-Host $folderURL

    #move folder
    Move-PnPFile -SourceURL $folderURL -TargetUrl "https://$($Domain).sharepoint.com/sites/PrivateEmployeeArchive/Shared Documents/Archived OneDrive/$User" -AllowSchemaMismatch -AllowSmallerVersionLimitOnDestination -Force -Connection $UserSiteConnection
}

#move each file to archive folder
ForEach($file in $UFiles){

    #get file url and format it in Documents/<filename> form
    $fileURL = "Documents$($file.ServerRelativePath.DecodedURL.split("Documents")[1])"

    Write-Host $fileURL

    #move file
    Move-PnPFile -SourceURL $fileURL -TargetUrl "https://$($Domain).sharepoint.com/sites/PrivateEmployeeArchive/Shared Documents/Archived OneDrive/$User" -AllowSchemaMismatch -AllowSmallerVersionLimitOnDestination -Force -Connection $UserSiteConnection

}

Write-Host "Done!"

OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,299 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,265 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,795 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Xyza Xue_MSFT 28,051 Reputation points Microsoft Vendor
    2025-01-28T06:54:44.28+00:00

    Hi @Riley B ,

    The issue of share links being broken and share permissions being removed after moving files is a real challenge.

    Unfortunately, the Move-PnPFile cmdlet in PnP PowerShell does not have a built-in option to retain share permissions or mimic the “Keep sharing with the same people” checkbox you see in the OneDrive UI.PnP PowerShell does not expose it directly as a simple command.

    Regarding your concern, to improve the Microsoft sync view and this type of change has come up. As we listen to all feedback and consider them for additional feature/future versions of our products, and we requested you send/provide feedback our Microsoft feedback portal. Your feedback helps us know which features are most important to you.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.