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
- delete files that you KNOW are not needed
- delete progressively “riskier” types of content until the required minimum free space is achieved
- delete files in the safest way possible by excluding certain subfolders and protecting known critical folders
- log everything
The solution logs the beginning and ending free space and cleans up the following items
- Windows memory dumps
- System temp files (C:\Windows\Temp)
- Windows Update downloads and ConfigMgr cache content for software updates
- Compress (compact) certain folders on NTFS volumes
Until the requested free space is achieved, the following tasks are run
- Purge ConfigMgr Client Package Cache items not referenced in 30 days
- Purge ConfigMgr Client Application Cache items not referenced in 30 days
- Purge Windows upgrade temp and backup folders
- Purge Windows Update Agent logs, downloads, and catalog
- Purge Windows Error Reporting files using Disk Cleanup Manager
- Purge Component Based Service files
- Cleanup User Temp folders: Deletes anything in the Temp folder with creation date over x days ago
- Cleanup User Temporary Internet Files
- Purge Content Indexer Cleaner using Disk Cleanup Manager
- Purge Device Driver Packages using Disk Cleanup Manager
- Purge Windows Prefetch files
- Purge Delivery Optimization Files
- Purge BranchCache
- Cleanup WinSXS folder
- Run Disk Cleanup Manager with default safe settings
- Purge System Restore Points
- Purge ConfigMgr Client Package Cache items not referenced in 1 day
- Purge ConfigMgr Client Package Cache items not referenced in 3 days
- Purge ConfigMgr Client Application Cache items not referenced in 3 days
- Purge C:\Drivers folder
- Delete User Profiles over x days inactive
- 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"