Part of Windows is an ancient tool that can clean your hard drive: cleanmgr.exe.

This tool can remove a variety of data garbage and at times removes many gigabytes of space. What makes it interesting for PowerShell is its support for automation.

To automate hard drive cleanup, first you need to launch PowerShell with Admin privileges (without them, no error is raised but your cleanup choices won’t be saved correctly). Next, pick a random number, i.e. 5388, then run this:

 
PS> cleanmgr.exe /sageset:5388 
 

This opens a dialog window where you can select which cleaner tasks you want to run in the future. Check all items that apply, then click OK.

Now these choices are stored in the Windows Registry under your custom ID 5388 (or whichever else you picked). Check by running the command again: the dialog should open again and remember your choices. If the dialog does not remember your choices, you may not have run it with Administrator privileges.

To automate hard disk cleanup, run the command with the parameter /sagerun instead of /sageset, and use the same ID number:

 
PS> cleanmgr /sagerun:5388
 

The cleaner now unattendedly performs the cleanup, so this can be a convenient way of cleaning certain aspects of data on your hard drives.

Note that a dialog shows the cleanup progress, and the current user can always abort the cleaning by clicking “Cancel”. There is unfortunately no way of hiding this dialog.




Twitter This Tip! ReTweet this Tip!

Anonymous