PowerShell Script to Restore SharePoint Recycle Bin Items for September 2024 by Specific User

Mohammed Thabsir 0 Reputation points
2024-12-11T07:00:46.0066667+00:00

Define the date range for September 2024

$date1 = Get-Date -Year 2024 -Month 9 -Day 30 # End of September 2024

$date2 = Get-Date -Year 2024 -Month 9 -Day 1 # Start of September 2024

Connect to SharePoint Online

Connect-PnPOnline -Url https:

Get and restore items from the Recycle Bin within the specified date range and by the specified user

Get-PnPRecycleBinItem | Where-Object {

($_.DeletedDate -ge $date2 -and $_.DeletedDate -le $date1) -and

($_.DeletedByEmail -eq '')

} | Restore-PnpRecycleBinItem -Force

  1. How do I properly connect to SharePoint Online using the PnP module (Connect-PnPOnline)?
    • Are there specific prerequisites or steps I should follow to authenticate and connect successfully?
    • What common issues are faced during connection, and how can I resolve them?
  2. Can this script handle larger Recycle Bin data without performance issues?
  3. Are there any improvements or optimizations you suggest for better error handling or logging?
  4. Is there a better way to validate the email or handle possible null values in DeletedByEmail?
Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 24,941 Reputation points Moderator
    2024-12-11T08:08:44.38+00:00

    Hi @Mohammed Thabsir,

    1. Starting to use PnP PowerShell consists out of 3 steps: Installing the PnP.PowerShell module Registering your own Entra ID Application Connecting and authenticating

    Here are some commom issues you may meet when executing Cpnnect-PnPOnline, you can take a reference to it. If you encounter others, you can post new threads here.

    1. The script can handle larger data sets, but performance may vary based on the amount of data and the environment. Consider implementing pagination or processing items in batches if you encounter performance issues.
    2. It's OK to use this script.
    3. Instead of checking for an empty string, you can use: $_.DeletedByEmail -eq $null

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.