Skip to content

Instantly share code, notes, and snippets.

@pohatu
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pohatu/3adabfe7239f5a04fc70 to your computer and use it in GitHub Desktop.
Save pohatu/3adabfe7239f5a04fc70 to your computer and use it in GitHub Desktop.
Run Disk Cleanup with your settings on a weekly cadence
# Automatically Run Disk Clean-Up Utility With Preferred Settings
# Windows 8.1/2012 R2; Powershell 4.0
# First, launch CleanMgr.EXE with /sageset: n - where n is some number (defaults to 5000)
# You will need to set the settings as you desire in the UI that appears
# When you save, it will save the settings in a regkey
# for more information, see http://support.microsoft.com/kb/315246
#
# The n value, which is stored in the registry, allows you to specify tasks for Disk Cleanup to run.
# The n value can be any integer value from 0 to 65535.
# 5000 is default because your temp files will go Audi-5000 :-)
$n=5000
# To have all of the options available when you use the /sageset option,
# you might need to specify the drive where Windows is installed.
& CleanMgr.EXE /sageset:$n /d ($env:SystemDrive)
# Now we'll create and install a scheduled task to make it run with your saved settings once a week.
# You can find your Scheduled Task in TaskSchedule UI under the name <yourusername>_Run_DiskCleanupTool
# See Also: http://technet.microsoft.com/en-us/library/jj649816.aspx
#
$sta = New-ScheduledTaskAction -Execute 'cleanmgr.exe' -Argument "/sagerun:$n" -WorkingDirectory ($env:SystemDrive)
$stt = New-ScheduledTaskTrigger -Weekly -WeeksInterval 1 -DaysOfWeek Sunday -At 3am
$st = New-ScheduledTask -Action $sta -Trigger $stt -Description "Run Disk Cleanup Tool"
Register-ScheduledTask -InputObject $st -TaskName "$($env:USERNAME)_Run_DiskCleanupTool"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment