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?

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

The first step is simply to navigate your browser to your team site’s homepage, click on the Settings gear in the top right corner, then 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. You’ll find the “Restoring 1 Item” Message and then “Restored 1 item” in the status area once completed.

sharepoint online restore deleted folder powershell

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, You may have to access the site collection recycle bin that holds all the files and folders which were deleted by any members of your team sites.

Restore deleted folder in SharePoint Online using PowerShell

Here is the PnP PowerShell script to restore a folder from recycle bin.

#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 | 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 specific a name from the recycle bin (regardless of their location)
  • What if you want to make sure 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 | 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
    } 
}

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply