Search and Restore file from SharePoint Online using PowerShell

When having SharePoint Online, one can easily recover items from the Recycle Bin, but unfortunately, there is no search facility for the Recycle Bin from the web interface. We can use PowerShell to easily search for the item.

To be able to connect, we need to install the new module, Open a PowerShell window and use the below to install the module.

Install-Module -Name PnP.PowerShell

If you would have an older version already installed, you need to use the below command to remove the old module, and then install the module as above.

Uninstall-Module -Name SharePointPnPPowerShellOnline -AllVersions -Force

To connect with the SharePoint Online site, we need to use the following command which will ask us to allow the script to communicate along with the Multi-Function Authentication (MFA).

Connect-PnPOnline -Url "Enter the SharePoint URL here" -Interactive

To extract the specified SharePoint Online Recycle Bin, you need to use the following command to output to the screen. Change the RowLimit to set the maximum results.

Get-PnPRecycleBinItem -RowLimit 50 | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Format-table -AutoSize

To export the results to a CSV, you need to add the Export-CSV at the end as below

Get-PnPRecycleBinItem -RowLimit 50 | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Export-Csv "C:\Temp\RecycleBin.csv" -NoTypeInformation

To restore a specific file, you cannot just specify the name, as you need to get the item information. We need to use the following commands

$itemtorestore=Get-PnPRecycleBinItem | ? -Property Title -EQ "filename of the deleted file"
Restore-PnpRecycleBinItem -Identity $itemtorestore -Force

This will restore the file to its original location.

Swish

Leave a Reply

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