OneDrive for Business: Restore All Files and Folder from Recycle Bin using PowerShell

Requirement: Restore all deleted files and folders from OneDrive for business using PowerShell.

PowerShell to Restore All Deleted Items in OneDrive for Business

OneDrive for business stores all deleted items in the Recycle Bin. If you accidentally delete an item, you can restore the item from the Recycle Bin. Recovering deleted items from OneDrive for Business Recycle bin through the web browser interface is explained in another post: How to restore Files and folders from OneDrive? In this post, let’s see how to use PowerShell to restore all your deleted files and folders from the recycle bin.

Please note, You must have site collection administrator rights to the OneDrive site before proceeding with the restore.

#Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount = "Salaudeen@Crescent.com" #UPN

Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $AdminCenterURL -Interactive

    #Get onedrive site URL of the user
    $OneDriveURL = Get-PnPUserProfileProperty -Account $UserAccount | Select -ExpandProperty PersonalUrl

    If($OneDriveURL -ne $null)
    {
        #Connect to OneDrive site
        Connect-PnPOnline -Url $OneDriveURL -Interactive

        #Get All items in the recycle bin
        $RecycleBinItems = Get-PnPRecycleBinItem -RowLimit 500000

        #Check if there are any deleted items in recycle bins
        If($RecycleBinItems.count -eq 0) {
            Write-host "No Items found in the recycle bin!" -f Yellow
            Break
        }

        #Restore all items from the recycle bin
        $RecycleBinItems = Get-PnPRecycleBinItem -RowLimit 500000
        ForEach($Item in $RecycleBinItems)
        {
            #Get the Original location of the deleted file or folder
            $OriginalLocation = "/"+$Item.DirName+"/"+$Item.LeafName
            If($Item.ItemType -eq "File")
            {
                $OriginalItem = Get-PnPFile -Url $OriginalLocation -AsListItem -ErrorAction SilentlyContinue
            }
            Else #Folder
            {
                $OriginalItem = Get-PnPFolder -Url $OriginalLocation -ErrorAction SilentlyContinue
            }
            #Check if the item exists in the original location
            If($OriginalItem -eq $null)
            { 
                #Restore the item
                Restore-PnPRecycleBinItem -Identity $Item -Force
                Write-host "Restored Item '$($Item.LeafName)' to '$($Item.DirName)'" -f Green
            }
            Else
            {
                Write-Host "A file or folder with this name $($Item.LeafName) already exists in '$($Item.DirName)', Skipping.."  -f Yellow
            }
        }
    }
    Else
    {
        Write-host "OneDrive site for the user doesn't exist!" -f Yellow
    }
}
Catch {
    Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Related post: Restore Deleted Files from Recycle Bin in SharePoint Online using PowerShell

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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