Skip to main content

This worklet will allow you to run the Windows Cleanup Tool on any of your Windows endpoints, based on the amount of free space remaining on the C: drive.



If you use the /SageSet and /SageRun commands, you’ll need to create presets on the endpoints first. This /SageSet process can be automated, as some folks have demonstrated here:






If you don’t want to bother with the /SageSet presets you can use the /VeryLowDisk flag instead, with the caveat that this will pop up the scan completion window on the endpoint, which might be confusing to users.



More documentation on the cleanmgr.exe flags here:






Evaluation code:



# Replace $MinDiskFree with whatever value you want as the minimum available space before running the cleanuptool

# The $MinDiskFree value is in bytes

# Only checks the C drive for space

$Diskfree = (Get-WmiObject -Class Win32_logicaldisk - Filter "DeviceID = 'C:'").freespace

$MinDiskFree = '5000000000'



if ($DiskFree < $MinDiskFree) {

Exit 0

} else { Exit 1 }



Remediation code:



# Assumes you've already setup a preset using the /SageSet flag, in the 1 preset.  If you use a different preset slot, then adjust accordingly.

# The /SageSet slots are available from 1 to 65535

# To set a preset, run the following command on your Windows endpoint: cleanmgr.exe /SageSet:1, and then select which options you want for cleanup

# After this is done, the following command will run with those presets and will not surface a message to the end user

Try {

cleanmgr.exe /Sagerun:1

Write-Output "Disk cleanup finished"

Exit 0

} catch {

Write-Output "Disk cleanup failed"

Exit 1

}

Do you have to run the cleanmgr.exe setup on each computer to get the /Sagerun:1 to work properly? I ran the worklet on my system but can’t tell if any files were deleted as the disk cleanup shows the same amount of files before and after the worklet ran. I received the output it finished but can’t be sure files were cleaned up.