PnP PowerShell is a powerful tool that allows you to interact with SharePoint and perform various operations, including restoring files from the recycle bin. In this blog post, we will explore how to use PnP PowerShell to restore files from the recycle bin, filter by path or folder, and filter by timespan.
Basic Commands
Here are some of the basic cmdlets you can use:
Get-PNPRecycleBinItem
: This cmdlet retrieves all items in the recycle bin.Restore-PnPRecycleBinItem
: This cmdlet restores the specified item from the recycle bin to its original location.
Here is an example of how you can use these cmdlets:
|
|
In this example, replace <ID>
with the ID of the recycle bin item you want to restore.
Filtering by Path or Folder
You can filter the items in the recycle bin based on their directory name or title. Here is an example:
|
|
In this example, Get-PnPRecycleBinItem
retrieves all items in the recycle bin. The ForEach-Object
cmdlet iterates over each item. If a file with the same path does not already exist, the script restores the item.
Filtering by Timespan
You can also filter items based on their deletion date. Here is an example:
|
|
In this example, Get-Date
gets the current date and time, and AddDays(-30)
subtracts 30 days from it. The Where-Object
cmdlet filters out items that were deleted more than 30 days ago. The script then restores the remaining items.
Restoring Files Deleted in Last 3 Hours from a Specific Folder
Here’s an example of how you can restore files that were deleted in the last 3 hours and were located in a certain folder:
|
|
In this example, replace <YourFolderPath>
with your specific folder path.
with something like this: $folderPath = "/sites/YourSiteName/Shared Documents/YourFolderName"
Please note that you need to be connected to your SharePoint site using Connect-PnPOnline
before running these commands.
Conclusion
PnP PowerShell provides a powerful way to manage your SharePoint site, including restoring files from the recycle bin. By understanding how to use these cmdlets and how to filter by path or folder and timespan, you can effectively manage your SharePoint site’s content.
Remember to always test your scripts in a safe environment before running them on your live site to avoid any unintended consequences.
Happy coding! 🚀