Microsoft365
PowerShell
In this blog post, learn how to restore a deleted OneDrive site in SharePoint Online using PowerShell.
Introduction
Prerequisites
- A SharePoint Online tenant administrator account
- PowerShell 7.2 or later installed on your computer
- The PnP PowerShell module installed on your computer
Script Overview
The “Get-deleted-OneDrives.ps1” script is a PowerShell script that uses the PnP PowerShell module to restore a deleted OneDrive site. Here’s an overview of what the script does:
- Connects to SharePoint Online using the
Connect-PnPOnlinecmdlet. - Retrieves a list of all deleted OneDrive sites using the
Get-PnPTenantDeletedSitecmdlet. - Filters the list of deleted OneDrive sites to find the site that matches the specified user name using the
Where-Objectcmdlet. - Restores the deleted OneDrive site using the
Restore-PnPTenantSitecmdlet. - Checks if the site was successfully restored using the
Get-PnPTenantSitecmdlet. - Disconnects from SharePoint Online using the
Disconnect-PnPOnlinecmdlet.
Let’s take a closer look at each step of the script.
| |
Step 2: Retrieve a List of Deleted OneDrive Sites
In the script, the -IncludeOnlyPersonalSite parameter is used to retrieve only deleted OneDrive sites.
| |
Step 3: Filter the List of Deleted OneDrive Sites
| |
Step 4: Restore the Deleted OneDrive Site
The fourth step of the script is to restore the deleted OneDrive site using the Restore-PnPTenantSite cmdlet. This cmdlet requires the URL of the deleted site and the -Force parameter to confirm the restoration.
In the script, the URL of the OneDrive site that was found in step 3 is used as the URL parameter for the Restore-PnPTenantSite cmdlet.
| |
Step 5: Check if the Site was Restored
The fifth step of the script is to check if the OneDrive site was successfully restored using the Get-PnPTenantSite cmdlet. This cmdlet requires the URL of the site that was restored.
In the script, the URL of the OneDrive site that was found in step 3 is used as the URL parameter for the Get-PnPTenantSite cmdlet.
The whole script looks like this:
| |
Conclusion
Comments
You can use your Mastodon account to reply to this post. Learn how this is implemented here.
Reply to ollimenzel’s post
With an account on the Fediverse or Mastodon, you can respond to this post.
Since Mastodon is decentralized, you can use your existing account hosted by another Mastodon server or compatible platform if you don’t have an account on this one.
Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.
Practical Graph: Create a Files Report for a OneDrive for Business Account
OneDrive is the Great Dumping Ground for Microsoft 365 Information

Reporting OneDrive Files
Adapting Script Code for OneDrive
Permissions
- Files.Read.All: Access files in any OneDrive account that the signed in user is allowed to view.
- RecordsManagement.Read.All: Read information about retention labels.
- InformationProtectionPolicy.Read: Read information about sensitivity labels.
Essentially, all the data about files ends up in a PowerShell list that we can query to slice and dice the information to discover different aspects of the content held in OneDrive.


Analyzing the Data
The basic information I want to know about a OneDrive account is:
- How many files are in the account.
- What kind of files are in the account.
- How much quota is used and how much remains.
- What file types occupy most space.
- The number of files that have retention and sensitivity labels.
I’m sure that your imagination will come up with other insights that could be generated from the data. That’s the joy of PowerShell: create data and then decide how to use it. Figure 1 shows the analysis for the points listed above. My OneDrive for Business account has existed since 2013, so it’s not hard to understand how some of the debris accrued.

Unsurprisingly, the account holds a lot of Word. PDF, and PowerPoint files. There’s also lots of PowerShell files, probably the remnants of some badly managed installations.

The reported files don’t include items in the recycle bin. A beta Graph API for listing items in the recycle bin is available that doesn’t seem to extend to OneDrive for Business.
You can download the complete script from GitHub.
Contemplating File Statistics
Overall, I struggle to use the 5 TB quota assigned to my OneDrive account. The availability of massive storage is certainly a justification for not bothering too much about the digital debris that accumulates over time alongside the valuable content in a OneDrive for Business account. After all, if Microsoft makes the storage available, why not fill it?
Seriously, it’s a good idea to perform regular housekeeping to keep debris under control. The report helps by exposing items that might remain invisible through the OneDrive GUI unless you go looking for specific information. I like that and have removed a thousand files since I started to develop the script. Maybe it can help you to clean up too.




