top of page

SHAREPOINT - LIST CONTENTS OF RECYCLE BINS

  • Writer: Jonathan Stuckey
    Jonathan Stuckey
  • 3 days ago
  • 5 min read

Audience: Solution Designer, Information Manager, IT Operations

Author: Jonathan Stuckey


This article provides a practical (tested) option for exporting reports about site recycle bin, for lifecycle management and disposal actions.


It's also a response to really annoying Answers on Microsoft support and community pages by 'experts' and MVPs who publish without testing or due-diligence on their responses.

Stressed person at a desk with scattered apps and icons, holding head in hands. Brown brick background, laptop, papers, pens visible.
Why? It doesn't take long to test

SharePoint reporting on the contents of site recycle bins

Some of the basics with managing your SharePoint environment are, frankly, a pain the proverbial Microsoft has long-held with the approach of "minimum viable' and fall-back to partners for "value-add" (read: essentials that Microsoft can't be bothered with).

When you are managing tenancy wide support, information management lifecycle and volume based tasks like Retention and Disposal and accurate understanding of the situation in Recycle bins is essential. As this is a Information Management need but we can't get anything helpful


Using tools

I am a big advocate of using the right tools for the job, and often most of these things are better left to just paying the money for something which:

  1. does the job

  2. somebody else maintains support

  3. doesn't cost more than hiring a developer for a week/month etc


Unfortunately for this particular little requirement main-stay options like ShareGate do not offer this level of reporting [Support statement]. Annoying, but true.


Other application providers with big-platforms and services can - but you are then into some very expensive and time-consuming effort for a lite (simple) reporting requirement.


The DIY approach with PowerShell

So we have to default to PowerShell for this simple requirement. Initial consideration - it does require the ability to run PowerShell 7 command shell, and utilise PnP.Powershell module in script.


You will need an administrative level of privilege on the site (or sites) to operate the script and output the reports


Basics are:

KEY: to use the PnP.Powershell module against SharePoint Online it needs to have been registered as an approved App in Entra App Registration first.
  1. Launch PowerShell v7 command prompt

  2. Import and Load the PnP.Powershell module for use

  3. Check you have the appropriate login credentials for authentication

  4. Set your PnP module app registration client Id variable for running

#Set powershell variable
$ClientId = "0a123456-01ab-6789-ab01-123a4567b890"
$siteUrl = "https://organisation.sharepoint.com/sites/<site-name>"

...where you replace my examples with details relevant for your tenancy and site.


  1. Connect to the site you need to report on, and run report-generation:

#Connect your session to specific site on
Connect-PnpOnline -ClientId $ClientId -Url $siteUrl -Interactive

#Tested and proven PnP command with data output
Get-PnPRecycleBinItem | Select-Object LeafName, DeletedDate, DirName | Export-Csv -Path "RecycleBinFullReport.csv" -NoTypeInformation

...this will give you the complete content listing, with just item name, deleted date, and original location in the site:


Table in Excel show list of files in a SharePoint stage 1 RecycleBin. 8 rows of date collected under columns for LeafName, DeletedDate and DirNam.
Example of report output from the Get-PnPRecycleBintItem command

For other useful variations you can use to report on a subset of Recycle bin content, based on first or secondstage:


Get-PnPRecycleBinItem -FirstStage | Select-Object LeafName, DeletedDate, DirName | Export-Csv -Path "RecycleBinFirstStageReport.csv" -NoTypeInformation

or substitute the Get-PnPRecycleBinItem output to provide batches of information for use in later actions etc.. you get the idea.


TIP: It is worth checking on a site by running the Get-PnPRecycleBinItem through a process of counting the total number of files held beforehand, particularly on sites which have a lot of content. Report generation can take a very long time on sites with 100k - 1m+ items being processed.


Creating a repeatable report from automated trigger


Architecturally this a basic pattern, but I don't see a lot of IT or partner support organisations offering management of this kind of thing for their customers. Large organisations can afford to retain staff that should know how to do this - but often don't. Small organisations are beholden to the internal IT super-hero (which is a bad plan for continuity support), or IT partner... see my first comment.

This section outlines the pattern, but I'm not doing your work for you.

Really what you need to do for repeatability and robustness in process is turn this into a some form of standard report, which can run on either:

  • a timer job, or

  • a trigger (usually from a list register or 'button' on dashboard)


Best way to run this is work with a good infrastructure engineer who understands:

  • Microsoft 365 access,

  • Azure/Entra applications,

  • the use of Automation accounts and

  • how to setup a Runbook.


Flowchart showing SharePoint's Recycle Bin and Job List connecting to Azure's Runbook. Compliance Team accesses the Report Library.
basic model for implementing trigger-based report generation

What you will need to be able to do is convert and import a version of the PowerShell as a Azure script to be executed by a Runbook

Basic pattern ingredients are:


  1. IT person who understands basics of Azure, Azure application registration and upload PowerShell scripts.

  2. Details of Azure | Entra subscription

  3. A Resource Group

  4. An Automation account (service account name that can run)

    1. check automation account has correct SharePoint privileges to run

    2. import Shared Resources > Modules for

      1. PnP.Powershell - min. version 3.1.0

      2. any other module scopes required

  5. Create a Runbook, set for appropriate PowerShell version

  6. SharePoint site and library location (URL) for uploading the exported report data

  7. A login or credentials which can access, upload and edit content on the report site

  8. [example] SharePoint list used to register request for report and que the job

    1. list structure to include attributes for:

      1. target site name,

      2. target site URL,

      3. schedule date for running job,

      4. target location for report (site and library name)

  9. update PowerShell to run using variables from data provided from the [example] trigger source - see 7.

  10. include 'upload' option to output the report-data to a file in report target location

    1. check SP site login credentials (person creating the report request) can access site for new report output


Notes

  • best-practice security and data management guidance is to use a different account for uploading report to SharePoint with only required privileges necessary


  • design recommendation for the SharePoint register is make report location a pre-set or default location - to minimise potential for data-entry errors in the job-request form


  • the original tasks is using PowerShell 7 Command shell, but Azure|Entra only supports specific versions for automation - so you will need to test this on: PowerShell Module version 7.2


  • must test and prove against the imported version of the modules supported under this version of PowerShell i.e. PnP.Powershell version 3.1.0


Office365 Automation portal showing module list with names, statuses, types, and versions. Sidebar options include Runbooks and Jobs.
Example: Azure Automation Account imported Modules for Runbook script execution

Resources

The annoying (unvalidated, untested) response which started this


Command reference on GitHub PnP


Disclaimer

Generative AI (Eraser.io) was used in the creation of the process diagram in this article. No other Generative AI was used for content creation. All content was created by author, based on released information from Microsoft and step-by-step testing and verification before committing to article.


Any errors or issues with the content in this article are entirely the authors responsibility.


About the author: Jonathan Stuckey

Comments


Commenting on this post isn't available anymore. Contact the site owner for more info.

©2024 by What's This...?

  • LinkedIn
  • YouTube
  • X
bottom of page