OneDrive for Business allows users to store, share, and manage files. Sometimes, files may be accidentally deleted, but Microsoft provides a Recycle Bin to restore them. Using PnP PowerShell, administrators can restore deleted OneDrive files efficiently.
What You’ll Learn:
Connect to a OneDrive account using PnP PowerShell
Retrieve deleted files from the OneDrive Recycle Bin
Restore specific or all deleted files
Empty the Recycle Bin if necessary
Step 1: Install and Import PnP PowerShell
If you haven’t installed PnP PowerShell, install it using:
Install-Module -Name PnP.PowerShell -Scope CurrentUser -AllowClobber -Force
After installation, import the module:
Import-Module PnP.PowerShell
PnP PowerShell is now ready!
Step 2: Connect to the User’s OneDrive
To restore deleted files, first connect to the user’s OneDrive site:
$OneDriveUrl = "https://yourtenant-my.sharepoint.com/personal/username_domain_com"
Connect-PnPOnline -Url $OneDriveUrl -Interactive
🔹 Replace “yourtenant” with your actual Microsoft 365 tenant name.
🔹 Replace “username_domain_com” with the OneDrive user’s UPN format.
🔹 You will be prompted to sign in as an Administrator or the respective user.
Connected to the user’s OneDrive!
Step 3: View Deleted Files in OneDrive Recycle Bin
To list all deleted files in the OneDrive Recycle Bin, run:
Get-PnPRecycleBinItem | Select-Object Title, ItemType, DeletedBy, DeletedDate, Id
Output Includes:
- Title → File name
- ItemType → Whether it is a File or Folder
- DeletedBy → Who deleted it
- DeletedDate → Date of deletion
- Id → Unique identifier for restoring
Deleted files retrieved successfully!
Step 4: Restore a Specific File
To restore a single file, use its Id from the previous step:
Restore-PnPRecycleBinItem -Identity "GUID-of-the-file"
🔹 Replace “GUID-of-the-file” with the actual file Id.
File restored successfully!
Step 5: Restore All Deleted Files
To restore all files in the OneDrive Recycle Bin:
Get-PnPRecycleBinItem | Restore-PnPRecycleBinItem
All deleted files restored successfully!
Step 6: Empty the OneDrive Recycle Bin (Optional)
If you want to permanently delete all files in the OneDrive Recycle Bin:
Get-PnPRecycleBinItem | Remove-PnPRecycleBinItem -Force
Warning: This action cannot be undone!
OneDrive Recycle Bin cleared!
Step 7: Disconnect from OneDrive
Once done, disconnect the session:
Disconnect-PnPOnline
Disconnected successfully!