Forum Discussion

Redbaron99's avatar
Redbaron99
Copper Contributor
Mar 27, 2022
Solved

Restore all files but ones that already exist from the recycle bin using PowerShell

Hello,

I have a client that I am working with that had a slight mix-up that lead to many duplicate files being created and then deleted (along with others that they need restored), there are 10,000s of files scattered amongst 100s of sub folders, so a manual process wouldn’t be possible. I know I can restore from the recycle bin by date, but does anyone know of a way that I could use that PowerShell cmdlet to also automatically say “no” to files that already exist?

NOTE: the files that are duplicates have the exact same file name and path as the existing ones, meaning that it would make them a “copy”

Any help would be appreciated!
  • Thank you for your patience, and help. I was able to figure out the issue. The "(Get-Date)" string applies a time to it, thus, making it fail. I worked around it by using a date range instead. The following cmdlet solved the issue:

     

    Install-Module -Name PnP.PowerShell
    Import-Module PnP.PowerShell
    connect-pnponline -url *sharepoint site URL* -interactive

    $date1 = (Get-Date).date.AddDays(-5)
    $date2 = (Get-Date).date.AddDays(-6)

    Get-PnPRecycleBinItem | ForEach-Object {
    $dir = $_.DirName
    $title = $_.Title
    $path = "$dir/$title"
    $deletedDate = $_.DeletedDate

    $fileExists = Get-PnPFile -url $path -ErrorAction SilentlyContinue

    if ($fileExists) {
    Write-Host "$title exists"
    } else {
    Write-Host "$title doesn't exist"
    if ($deletedDate -gt $date2 -and $deletedDate -lt $date1) {
    Write-host "$title is equal to $date2 and will be restored"
    $_ | Restore-PnpRecycleBinItem -Force -ErrorAction SilentlyContinue
    } else {
    Write-Host "$title is not equal to $date2 and will not be restored"
    }
    }
    }

     

  • Leavii's avatar
    Leavii
    Brass Contributor

     

    Redbaron99 

     

    Hello,

    I assume you are meaning from SharePoint Recycle bin.

    I quickly threw together a site, files, and this script and it worked when I deleted files and restored them. I included some resources as well so you could make a better/cleaner script for your need.

     

     

    connect-pnponline -url https://your.sharepoint.com/ -interactive
    
    $recycleBin = Get-PnPRecycleBinItem
    
    $recycleBin | ForEach-Object {
        $dir = $_.DirName
        $title = $_.Title
        $path = "$dir/$title"
    
        $fileExists = Get-PnPFile -url $path -ErrorAction SilentlyContinue
    
        if ($fileExists) {
            Write-Host "$title exists"
        } else {
            Write-Host "$title Restoring"
            $_ | Restore-PnpRecycleBinItem -Force
        }
    }
    
    Read-Host "Done"

     

     

    Sources
    SharePointDiary

    Github

    pnp.github/restore-pnprecyclebin

    pnp.github/get-pnprecyclebinitem

    • Redbaron99's avatar
      Redbaron99
      Copper Contributor
      Thank you so much, I’ll mess around with it for a bit. Am still learning PowerShell so thank you for the resources as well!
      • Leavii's avatar
        Leavii
        Brass Contributor
        Of course. Always glad to help. Let us know how it goes, eh? 😄

Resources