SharePoint Online: Restore a Deleted Folder using PowerShell

Requirement: Recover a deleted folder in SharePoint Online.

How to Recover 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 recover a deleted folder, as long as it’s less than 93 days since it 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:

  1. The first step is to navigate your browser to your SharePoint site’s homepage
  2. Click on the Settings gear in the top-right corner, then click on the “Site Contents” link.
  3. 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
  4. 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 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

It’s also possible to restore folders using PowerShell scripts! You can access the recycle bin, identify the deleted folders you want to restore, and restore them along with their contents. This can be a helpful way to save time and effort when you need to recover multiple deleted folders, or to automate the recovery process as part of a larger workflow.

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?

SharePoint Online: Recover a deleted folder

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?

Conclusion

In summary, it is often possible to recover a deleted folder in SharePoint Online by accessing the recycle bin and restoring the folder and its contents. By following the steps outlined in this guide, you can quickly and easily recover a deleted folder and get back to work. It’s also possible to restore a deleted folder in SharePoint Online using PowerShell to recover deleted items in bulk or to automate the recovery process. It is important to note that the recycle bin has a limited period of 93 days, and items in the recycle bin are automatically deleted after a certain period.

How to prevent folder deletion in SharePoint Online?

To prevent files and folders deletion in SharePoint Online, create a new permission level, “Contribute without delete”, and assign it to users and groups.
More info: How to Create a Permission Level for Contribute without Delete?

How to restore deleted site collections in SharePoint Online?

To recover a deleted site, Login to the SharePoint Admin Center >> Expand Sites >> Deleted Sites >> Pick the site you would like to restore and click on the “Restore” button. You can also use PowerShell to restore a site collection in SharePoint Online.
More info: Restore deleted site collection in SharePoint Online

How do I recover a deleted file in SharePoint Online?

Recycle bin can help you to recover deleted items for up to 93 days. To restore deleted files, Navigate to the SharePoint site >> Click on the Settings gear icon >> Choose the “Site Contents” >> click on the Recycle Bin >> Select the items you want to restore on the Recycle Bin page and click the “Restore” button.
More info: How to Recover deleted files 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. Required fields are marked *