Restore OneDrive files in bulk from Preservation Library - Powershell

Capt America 0 Reputation points
2024-11-07T20:09:27.5433333+00:00

Is there a way to pull files from the OneDrive Preservation library in bulk?

User had two computers and had OneDrive sync'd on both.

While in the process of issues they lost their OneDrive files and had cleared the trash.

They are all now located in the preservation library and need to restore most if not all.

Is there a method or script to expedite this? (bulk restore)

OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,155 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,849 questions
OneDrive Management
OneDrive Management
OneDrive: A Microsoft file hosting and synchronization service.Management: The act or process of organizing, handling, directing or controlling something.
1,279 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,561 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,608 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 18,175 Reputation points Microsoft Vendor
    2024-11-08T03:02:28.8433333+00:00

    Hi @Capt America,

    First, make sure your OneDrive has retention policies enabled. If you're unsure, you can contact your Purview administrator to confirm.

    Second, open OneDrive Preservation Hold Library:

    • Go to your OneDrive settings.
    • Click on "Return to classic OneDrive" at the bottom left.
    • Click on the settings gear icon again and select "Site Contents".
    • You should see the Preservation Hold Library listed there.

    You can select the files you want to restore and click Copy to button to copy the files to the location you want to restore.

    Or you can use PnP PowerShell to restore the files:

    This site where OneDrive save Preservation Hold Library in is similar to the SharePoint site and we can try to recover the files using the command of the SharePoint. Just need to replace it with OneDrive site url.

    # Define Parameters
    $SiteURL = "https://contoso-my.sharepoint.com/personal/username_contoso_onmicrosoft_com/"
    $ListName= "Preservation Hold Library"
    $User = "username@contoso.onmicrosoft.com"
     
    # Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
     
    # Get deleted Items that match conditions
    $DeletedItems = Get-PnPListItem -List $ListName -PageSize 2000  | Where {$_.FieldValues.Modified_x0020_By -like $User} | Sort-Object $_.FieldValues.PreservationDatePreserved -Descending
     
    #Iterate through each item in the preservation hold library and restore to original location
    ForEach($Item in $DeletedItems) {  
        Try {
            $OriginalURL = $Item.FieldValues.PreservationOriginalURL
            Write-host -f Yellow "Copying item:"$OriginalURL
            
            $SourceUrl = $Item.FieldValues.FileRef
     
            #Check if the File exists Already
            $FileExists = Get-PnPFile -Url $OriginalURL -ErrorAction SilentlyContinue
             
            If($FileExists)
            {
                Write-host -f Yellow "`tFile Already Exists!"  
            }
            Else
            {
                #Copy the file to its original location
                Copy-PnPFile -SourceUrl $SourceUrl -TargetUrl $OriginalURL -Force -ErrorAction Stop
                Write-host -f Green "`tFile Restored:" $OriginalURL
            }
        }
        Catch {
            Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
        }
    }
    

    Reference: SharePoint Online: How to Recover Deleted Files from Preservation Hold Library?

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link. 


    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.