Cleanup System Disk Files | Quisitive
Cleanup System Disk Files
February 17, 2021
Quisitive
In this blogs, we give you the best solution for cleaning up unnecessary files on Windows computers to free up disk space. 

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.


#.SYNOPSIS
#	Clean-SystemDiskSpace.ps1
#	Remove known temp and unwanted files
#.DESCRIPTION
#   ===== How to use this script =====
...
#.PARAMETER MinimumFreeMB
#.PARAMETER FileAgeInDays
#.PARAMETER ProfileAgeInDays
...
Write-LogMessage -Message "Attempting to get $('{0:n0}' -f $MinimumFreeMB) MB free on the $env:SystemDrive drive"
$StartFreeMB = Get-FreeMB
Write-LogMessage -Message "$('{0:n0}' -f $StartFreeMB) MB of free disk space exists before cleanup"
#Purge Windows memory dumps.  This is also handled in Disk Cleanup Manager
Remove-File -FilePath (Join-Path -Path $env:SystemRoot -ChildPath 'memory.dmp')
Remove-Directory -Path (Join-Path -Path $env:SystemRoot -ChildPath 'minidump')
 
#Purge System temp / Windows\Temp files
Remove-DirectoryContents -CreatedMoreThanDaysAgo $FileAgeInDays $([Environment]::GetEnvironmentVariable('TEMP', 'Machine'))
#Purge Windows Update downloads
Remove-WUAFiles -Type 'Downloads' -CreatedMoreThanDaysAgo $FileAgeInDays
Remove-CCMCacheContent -Type SoftwareUpdate -ReferencedDaysAgo 5
 
#Compress/compact common NTFS folders
Compress-NTFSFolder
If (-not(Test-ShouldContinue)) { Write-LogMessage 'More than the minimum required disk space exists.  Exiting'; Exit 0 }
##################### Cleanup items if more free space is required #############
If (Test-ShouldContinue) { #Purge ConfigMgr Client Package Cache items not referenced in 30 days
	Remove-CCMCacheContent -Type Package -ReferencedDaysAgo 30
}
If (Test-ShouldContinue) { #Purge ConfigMgr Client Application Cache items not referenced in 30 days
	Remove-CCMCacheContent -Type Application -ReferencedDaysAgo 30
}
...
$EndFreeMB = Get-FreeMB
Write-LogMessage -Message "$('{0:n0}' -f $($EndFreeMB - $StartFreeMB)) MB of space were cleaned up"
Write-LogMessage -Message "$('{0:n0}' -f $EndFreeMB) MB of free disk space exists after cleanup"