SharePoint Online: Restore a Deleted Folder using PowerShell

Requirement: Restore a deleted folder in SharePoint Online

How to restore a deleted Folder in SharePoint Online?

If you’ve ever mistakenly deleted a folder in SharePoint Online, you know the panic that sets in. Thankfully, it’s possible to restore a deleted folder, as long as it’s less than 93 days since the folder was deleted (and not removed from the second stage recycle bin!). In this article, we’ll walk you through the different options you have for restoring a deleted folder in SharePoint Online.

To restore a deleted folder in the SharePoint Online document library, follow these steps:

  • The first step is to navigate your browser to your SharePoint site’s homepage
  • Click on the Settings gear in the top-right corner, then click on the “Site Contents” link.
  • On the site contents page, click on Recycle bin link. You can also get into the recycle bin under the quick launch section if it is available.restore a deleted folder sharepoint online
  • Select the folder to restore from the recycle bin and click on the “Restore” button from the toolbar. sharepoint online restore deleted folder powershell

Once completed, you’ll find the “Restoring 1 Item” Message and then the “Restored 1 item” Message in the status area.

If you don’t see the deleted folder in the recycle bin, There are chances it may be in the second-stage recycle bin. Another probability is that you may have to access the site collection recycle bin that holds all the files and folders deleted by any other member of your team site.

Restore deleted folder in SharePoint Online using PowerShell

Here is the PnP PowerShell script to restore a folder from recycle bin. This nifty script can be helpful if you accidentally delete a folder or if it has been deleted by someone else, and you need to recover it.

#Parameters
$SiteURL = "https://Crescent.sharepoint.com/sites/Operations"
$FolderName = "Classified"
$ParentFolderURL = "sites/Operations/Shared Documents"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get the Deleted Folder
$DeletedFolder = Get-PnPRecycleBinItem -RowLimit 500000 | Where { $_.Title -eq $FolderName -and $_.DirName -eq $ParentFolderURL -and $_.ItemType -eq "Folder"}
 
#Restore Folder
If($DeletedFolder)
{
    Restore-PnPRecycleBinItem -Identity $DeletedFolder -Force -ErrorAction Stop
    Write-Host -f Green "Folder '$FolderName' restored successfully!"
}
Else
{
    Write-host -f Yellow "Could not Find a deleted Folder '$FolderName' deleted from '$ParentFolderURL'"
}

If you want to restore all deleted folders from a specific folder/subfolder/library, set the $ParentFolderURL parameter accordingly.

There are some limitations in the above script:

  • What if you want to restore all deleted folders with a specific name from the recycle bin (regardless of their location)
  • What if you want to ensure the deleted folder was not re-created in its original location?

Well, Here is the PowerShell that answers the above questions.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$FolderName = "Classified"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Get the Deleted Folder -  sort by most recently deleted
$DeletedFolders = Get-PnPRecycleBinItem -RowLimit 500000 | Where { $_.Title -eq $FolderName -and $_.ItemType -eq "Folder"} | Sort-Object -Property DeletedDate -Descending

#Stop executing the script if the deleted folder was not found in the recycle bin
If($DeletedFolders -eq $null)
{
    Write-host -f Yellow "Could not Find a deleted Folder '$FolderName' in recycle bin"
    return
}

#Restore all deleted folders to its original location
ForEach($Folder in $DeletedFolders)
{
    #Check if the folder exists in the original location
    $OriginalLocation = "/"+$Folder.DirName+"/"+$Folder.LeafName
    $OriginalFolder = Get-PnPFolder -Url $OriginalLocation -ErrorAction SilentlyContinue

    #Restore the Folder if it doesn't exists
    If($OriginalFolder -eq $null)
    {
        Restore-PnPRecycleBinItem -Identity $Folder -Force -ErrorAction Stop
        Write-Host -f Green "Folder '$($Folder.Title)' restored successfully to $($Folder.DirName)"
    }
    else
    {
        Write-Host "There is an another folder with the same name at '$($Folder.DirName)'. Skipping '$($Folder.LeafName)'" -f Yellow
    } 
}

After 93 days, the deleted items will be removed permanently, and you may need a Microsoft Support case to recover that deleted folder. Having a retention policy to preserve deleted items is also a good idea! How to Create a Retention Policy to preserve Deleted Items in SharePoint Online?

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.