Skip to content

disk_cleanup.ps1

Corey Watson edited this page Dec 1, 2025 · 1 revision

disk_cleanup.ps1

Windows disk cleanup automation with targeted cleanups.

Overview

Runs Windows Disk Cleanup utility on all fixed local drives, plus targeted cleanups for SoftwareDistribution, WinSxS, and Search Index to free up disk space.

Purpose

Automates comprehensive disk cleanup for managed Windows endpoints, including system-level caches and component stores that require elevated privileges.

Prerequisites

  • Windows 10/11 with cleanmgr.exe and DISM
  • WMI service running
  • Administrator privileges for DISM/services

Configuration

Required Inputs

  • CleanupProfile: Cleanup aggressiveness level (default: 'verylow')

Settings

  • DriveType filter: 3 (fixed local disks only)
  • Cleanups run sequentially to avoid conflicts
  • DISM targets system-wide (C: only)

Behavior

  1. Checks/elevates to admin
  2. Queries WMI for fixed drives
  3. Runs cleanmgr.exe per drive with /verylowdisk flag
  4. Performs targeted cleanups on C: (SoftwareDistribution, WinSxS, Search Index)
  5. Reports progress/completion

Security Notes

  • No secrets in logs
  • Targeted deletions only (no user files)

Exit Codes

  • 0: Success
  • 1: Failure (validation/WMI/admin check)

Example Output

[ INPUT VALIDATION ]
--------------------------------------------------------------
Cleanup Profile : verylow
Running as Admin : True

[ OPERATION ]
--------------------------------------------------------------
Querying fixed local drives...
Found 2 drive(s) : C:, D:
Running cleanup for drive C: (cleanmgr)
Running cleanup for drive D: (cleanmgr)
Running SoftwareDistribution cleanup...
Running WinSxS cleanup via DISM...
Rebuilding Search Index...

[ RESULT ]
--------------------------------------------------------------
Status : Success
Drives Processed : 2
Targeted Cleanups : 3/3

[ FINAL STATUS ]
--------------------------------------------------------------
Disk cleanup completed successfully. Reboot recommended for full effect.

[ SCRIPT COMPLETED ]
--------------------------------------------------------------

Version History

  • v1.1.0 (2025-11-29): Added SoftwareDistribution, DISM WinSxS, Search Index; admin check
  • v1.0.0 (2025-10-28): Initial implementation

Links

Clone this wiki locally