Clean-SystemDiskSpace

There have been many scripts and solutions written for cleaning up unnecessary files on Windows computers to free up disk space.  I’ve used several over the years, but nothing out on the interwebs fit what I thought was the best approach which is

  1. delete files that you KNOW are not needed
  2. delete progressively “riskier” types of content until the required minimum free space is achieved
  3. delete files in the safest way possible by excluding certain subfolders and protecting known critical folders
  4. log everything

The solution logs the beginning and ending free space and cleans up the following items

  1. Windows memory dumps
  2. System temp files (C:\Windows\Temp)
  3. Windows Update downloads and ConfigMgr cache content for software updates
  4. Compress (compact) certain folders on NTFS volumes

Until the requested free space is achieved, the following tasks are run

  1. Purge ConfigMgr Client Package Cache items not referenced in 30 days
  2. Purge ConfigMgr Client Application Cache items not referenced in 30 days
  3. Purge Windows upgrade temp and backup folders
  4. Purge Windows Update Agent logs, downloads, and catalog
  5. Purge Windows Error Reporting files using Disk Cleanup Manager
  6. Purge Component Based Service files
  7. Cleanup User Temp folders: Deletes anything in the Temp folder with creation date over x days ago
  8. Cleanup User Temporary Internet Files
  9. Purge Content Indexer Cleaner using Disk Cleanup Manager
  10. Purge Device Driver Packages using Disk Cleanup Manager
  11. Purge Windows Prefetch files
  12. Purge Delivery Optimization Files
  13. Purge BranchCache
  14. Cleanup WinSXS folder
  15. Run Disk Cleanup Manager with default safe settings
  16. Purge System Restore Points
  17. Purge ConfigMgr Client Package Cache items not referenced in 1 day
  18. Purge ConfigMgr Client Package Cache items not referenced in 3 days
  19. Purge ConfigMgr Client Application Cache items not referenced in 3 days
  20. Purge C:\Drivers folder
  21. Delete User Profiles over x days inactive
  22. Purge Recycle Bin

Get the full script and latest version on GitHub.

P.S. The script should work on Windows 7 SP1 with PowerShell 2.0, but I haven’t tested that scenario in awhile.  All recent testing has been with PowerShell 5.1.

Cleanup System Disk Files
Tagged on:                                 
%d bloggers like this: