CLEANMGR.exe

Syntax
CLEANMGR [/d driveletter] option
CLEANMGR [/d driveletter] /sageset:n option
CLEANMGR [/d driveletter] /sagerun:n option
Options
/d driveletter: Select the drive that you want Disk Cleanup to clean.
/sageset:n Display the Disk Cleanup Settings dialog box and create
a registry key to store the settings you select.
The n value is stored in the registry and allows you to
specify different tasks for Disk Cleanup to run.
n can be any integer from 0 to 65535.
Specify the %systemroot% drive to see all the available options.
/sagerun:n Run task 'n'
All drives in the computer will be enumerated, and the
selected profile will be run against each drive.
/TUNEUP:n Undocumented, appears to be the same as /sageset
/LOWDISK Disk Cleanup will open with all options checked by default.
Normally invoked when Windows notifies that of disk space is running out.
/VERYLOWDISK Run with the default settings, no user prompts.
/SETUP Analyze the system files left over from a previous Windows version. [Undocumented]
/Autoclean Analyze the system files left over from a previous Windows version/in-place upgrade.
and also remove those files automatically. [Undocumented]
/Help Display help
/Usage Display help
/? Display help

/sageset and /sagerun cannot be combined in a single command.

If CLEANMGR is run without /sageset or /sagerun, it will start by calculating how much disk space could be saved. This makes the process much slower to run.

Some cleanup options require elevation, when run without elevation the GUI will include an option [Clean up System Files] which will relaunch CLEANMGR in Elevated mode.

Examples

Choose a set of options for cleanup (this will display the GUI) and Save as settings group #64:

Items in bold can appear in more than one drive, the items marked with * can be viewed in the GUI.

Run the saved settings group #64

CLEANMGR /sagerun:64

To run Disk Cleanup this way regularly, you can create a shortcut which can be run from the Windows Startup folder or via a regular schedule. To run the saved settings group #64

%systemroot%\system32\cmd.exe /c CLEANMGR /sagerun:64

Application Data

Many files in Application Data hold system data that should not be deleted, however some applications do leave files which you can delete from a roaming profile, these can be selectively removed with a VBScript like this.

Recent files

The 'User Profile/Recent' folder (for Start, Documents) can contain many more shortcuts than are set to display in the GUI. A very large number of these can affect logon/logoff times.
To clear out the shortcuts:

cd %userprofile%\Recent
echo y| del *.*

Close Locked files by restarting Windows Explorer

Close all applications
Open a command prompt
Type:taskkill /im explorer
This should kill explorer and bring up a logout/shutdown dialogue box.
Simultaneously press CTRL+SHIFT+ALT.
While you keep these keys pressed, click [Cancel] in the Shut Down Windows dialog
box.
In the command prompt window, you should now be able to delete the locked
files.
At the command prompt, type explorer, and press ENTER to restart Windows Explorer.

To enable Cleanmgr on Windows 2008, open Server Manager and choose Add feature, then select "Desktop Experience"
After running cleanmgr on a server you will probably want to disable
"Desktop Experience" again (or use this script.)
When necessary cleanmgr can take ownership of the files before deleting them.