2

I'm building an script in PowerShell that runs via an RMM tool, so it executes in the SYSTEM context. The goal is to automatically delete files from the Recycle Bin that are older than 30 days, for all user accounts on a Windows 10/11 system.

I've tried:

  1. Iterating through C:\$Recycle.Bin\S-1-5-... SID folders

  2. Filtering $I* and $R* files using LastWriteTime

  3. Using Shell.Application.Namespace(0xA) (only works for the interactive user, not SYSTEM)

  4. Testing with full permissions and recursion

Problems:

  1. Even when files are present in the Recycle Bin, nothing gets deleted

  2. LastWriteTime doesn’t reliably reflect the deletion date

  3. SYSTEM has access but can't determine which files are old enough

  4. No clear or consistent metadata to filter on

Is there any reliable way to clean up Recycle Bin files older than 30 days across all users from a non-interactive SYSTEM context?

1
  • 2
    Windows will do this for you with Storage Sense. Go to Settings | System | Storage | Storage Sense. If you have an AD environment you can use a GPO to set it up. This page might help. Commented Nov 23 at 23:48

1 Answer 1

3

Preface:

  • This is not a direct answer to your question.

  • Instead, it explains why what you're trying to do may not be necessary / may not be worth doing; instead, either:

    • rely on built-in defaults, which apply as of (at least) Windows 11 25H2 (not sure about earlier versions or the - now out of support - Windows 10)

    • use configuration to make the client workstations perform the desired tasks themselves, automatically.


  • As of (at least) Windows 11 25H2, Windows appears to be configured to delete files from the recycle bin if they've been there for over 30 days by default via its Storage Sense feature, as the following screenshot of the Settings app shows:

    • Screenshot of Storage Sense in the Settings app
  • If that doesn't (fully) apply to your scenario, you can use configuration to enact the desired settings:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.