Help with script to automate selections in disk cleanup

# Help with script to automate selections in disk cleanup

• 2012年5月1日 13:58

Hello All,

I use a script to run disk cleanup in task manager(cleanmgr.exe /sagerun:n (where "n" is any number - I use 99)). After I drop this in task manager I then have to manually configure the settings in disk cleanup using "cleanmgr.exe /sageset:99". What I am looking for is a way to script the selection of the boxes that you need to check or uncheck during manual configuration of cleanmgr.exe /sageset:n. If I had a script that would do this, I could use group policy to deploy a sript to load all these settings without the manual intervention. Then I would use GPP to set the schedule of when clean manager runs. That is the goal - to automate this so I don't have to manually configure this on almost 200 workstations and laptops. Any help would be greatly appreciated.

Sincerely,

Brian

### 全部回复

• 2012年5月1日 14:14

You don't need to script anything.  Just set up the Scheduled Task in GPO Preference settings.

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)

• 2012年5月1日 14:42

Thanks for your reply Grant,

I know I can schedule the the task to run disk cleanup with GPP. What I am looking for is changing the settings in disk cleanup. They have default settings and I want to change them before running disk cleanup with task manager through GPP. I have to manually change those settings using "cleanmgr.exe /sageset:n". Please clarify if I am not understanding you correctly.

thank you,

Brian

• 2012年5月1日 14:50

All I'm saying is that you can set those parameters in a scheduled task.  Or am I still missing something.

Grant Ward, a.k.a. Bigteddy

What's new in Powershell 3.0 (Technet Wiki)

• 2012年5月1日 19:43

Lets get the terminology correct and then you two will be on teh same page.

hammerhead - tadk amanager is what we call the utlilty that shows all runiing process etc.  it runs as "Taskmgr: at a prompt an displays the GUI.

What I believe you are referring to is "Task Schefuler"  That runs as "SchTasks".

The arguments you are sending to the program can be sent as BigTeddy has noted.

¯\_(ツ)_/¯

• 2012年5月1日 19:59

Hi Grant,

If you go to the command prompt and type "cleanmgr.exe /sageset:99" it brings up "Disk Cleanup Settings". From there you select or deselect all the items that you want disk cleanup to "clean up". Then it keeps those settings in the registry. So when you run "cleanmgr.exe /sagerun:99" from task scheduler it will run disk cleanup according to the settings that you manually configured when you ran cleanmgr.exe /sageset:99. The goal is to create those registry settings automatically from a script that reflect what I have to change manually in "Disk Cleanup Setting" and avoid manually running sageset on each workstation. I am trying to streamline the process if possible:) Any suggestions on how to do this would be appreciated.

thank you,

Brian

• 2012年5月1日 20:04

To run a particular set of selections use 'sagerun'.  If you set teh selectiuon with sageset:99 then this will reacl and use those saved selections.

cleanmgr /sagerun:99

¯\_(ツ)_/¯

• 2012年5月1日 20:13

Thanks for seeing that JRV. Yes I am referring to task scheduler. I responded to BigTeddy, too, but running cleanmgr.exe /sageset:n (sageset:99 in my case) only brings up the "Disk Cleanup Setting" box. I then have to manually make the changes, which are then saved in the registry. Then when cleanmgr.exe /sagerun:99 is run, it will clean up the items that I manually selected. I am trying to automate this process.

thanks

Brian

• 2012年5月1日 20:37

Thanks for seeing that JRV. Yes I am referring to task scheduler. I responded to BigTeddy, too, but running cleanmgr.exe /sageset:n (sageset:99 in my case) only brings up the "Disk Cleanup Setting" box. I then have to manually make the changes, which are then saved in the registry. Then when cleanmgr.exe /sagerun:99 is run, it will clean up the items that I manually selected. I am trying to automate this process.

thanks

Brian

Save different sets of settings under differnt numbers.  Use sagerun in tasksced to run a particular set of settings.  YOu do not have to run sageset every time.  Ti is exacrtly what the cleanup wizard does when it buids you cleanup schedule.  It asks you what you want to do and how often and saves it in your registry then schedules the command with /sagerun:n.

Its a no brainer.  Why do you think you have to run sageset everytime?

¯\_(ツ)_/¯

• 2012年5月1日 20:57

JRV,

The goal is to apply this via Group Policy to close to 200 workstations and laptops. Cleanmgr.exe /sageset:n is a way to set parameters for that individual workstation that you run it on. Instead of physically visiting every workstation or having to manually enter these settings with every new workstation deploy I was hoping toautomate the process of deploying the settings we want to cleanup on a regular basis. My goal is to be able to use a script to automatically place the those preferred disk clean up settings on each workstation- if that's possible to script. I hope this makes sense.

thanks

Brian

• 2012年5月1日 21:24

Can't be done. It is basically a home user tool and not an Enterprise tool.

You could use GP preferences to copy the reg values to all workstations and use a preferences defined scheduled task to run this once a week.

I have hundreds of desktops and have never had to use a tool like that.

I always move all profiles or the network.  I do not allow logon scripts except where absolutely necessary and we can easily delete the local profile periodically to clean the system, as the roamed profile will recreate the local profile on next logon with a clean set of local folders.

I can go to an network and take a sledge hammer to any work station and lose nothing.  Just plug in a new workstation. Image it and have the user logon.  We never store files locally. Users cannot write anywhere on the local machine except to the profile.

We can also empty all trash bins remotely and delete then contents of the temp folders remotely.

If you want more aggressive cleaning than that use either PerfectDisk or diskmanager. It can manage all of your storage remotely b y compacting and compressing files and restructuring the system volume.   The new systems are so big and fast that this is now mostly unnecessary.  I can get a workstation with 500gb and 6 Gb of memory for about $700. More than enough space and speed to not need optimizing ever. ¯\_(ツ)_/¯ • 2012年5月2日 21:10 thanks JRV. Are you using GPP to empty all trash bins remotely and delete then contents of the temp folders remotely? If so what is your process? thanks Brian • 2012年5月2日 21:20 版主 Probably most sys admins don't bother unless the drive runs low on space for some reason. Bill • 2012年5月2日 21:30 Just using a delete command from a PowerShell in a loop. dir \\alpha\c$\recycler\*  -recurse

Win 7

dir '\\ws101\c$\$recycle.Bin\*' -force -recurse

We can also place a line in a logon script to empty the users temp folder very easily.

System temp is:

Remove-Item \\server01\c$\windows\temp\* -force -recurse -confirm:$false -ea 0

The is ;ittel need in a cooporate system, to do most of the other cleanup tasks.  IN all newer hardware we are not retrited as much in disk space.  I jsut orgherd a Win7 System which had as a miimum dis a 500Gb disk. FOr another \$70 I upgraded to a 1Tb disk.  Since we save nothing on workstations I don't have to do cleanups like I used to do 5 years ago.

¯\_(ツ)_/¯

• 2012年5月4日 19:59

Hi AbqBill,

For us we have multiple intranet applications that can create a lot of files in the users temp directory. We want to keep temp IE files to a minimum, too. Most of our workstations are 6 to 7 years old and we felt cleaning up those folders on a regular basis will help eliminate potential issues that we have run into before with this type up clutter.

thanks

Brian

• 2012年5月4日 20:01

thanks for the suggestions jrv. We'll see if this can fit our scenario.

Brian

• 2012年5月4日 20:05
版主

Hi,

IIRC you can configure IE caching through group policy. For the temp directory, you could schedule a logon script like this one:

@echo off
setlocal enableextensions
pushd "%TEMP%"
rd /q /s . 2> NUL
endlocal


This short .cmd script deletes everything in the current user's temp directory.

Bill

• 2012年5月4日 20:24

Hi AbqBill,

For us we have multiple intranet applications that can create a lot of files in the users temp directory. We want to keep temp IE files to a minimum, too. Most of our workstations are 6 to 7 years old and we felt cleaning up those folders on a regular basis will help eliminate potential issues that we have run into before with this type up clutter.

thanks

Brian

As Billsays youc an control IE through GP.  IE limits its cache.  There si really no point in modifying it.  It set the cache as a percentage of the hard drive.  Thi suffices for nrearly all things.

Try to not overmanage your systems. This is a fundamental rule of administration.  Use Group Policy and keep refining it until it is where it needs to be .  After that only react to exceptions.

Let the systems do their thing.  Only make changes where you know that you need the change.

Be proactive by monitoring the eventlogs.  They will provide you with clues to the health of your systems.  Be sure to do an EL cleanup before you even start worrying about disk space.  While you are flushing disks your network could be crashing.

¯\_(ツ)_/¯

• 2012年5月4日 20:25

thanks Bill, I am testing that and it clears most items out of the current users, temp file.

Sincerely,

Brian

• 2012年5月7日 12:49

thanks JRV
• 2012年5月10日 12:42

FYI here is one I use in vbs

Const HKEY_LOCAL_MACHINE = &H80000002
Dim objWSHShell		: Set objWSHShell = Wscript.CreateObject("Wscript.Shell")
Dim objReg 		: Set objReg = GetObject("winmgmts:" & "{impersonationLevel=Impersonate}!\\.\root\default:StdRegProv")
'// Create Clean up profile #64
objReg.SetDwordValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders","StateFlags0064",2
objReg.SetDwordValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files","StateFlags0064",2
objReg.SetDwordValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin","StateFlags0064",2
objReg.SetDwordValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files","StateFlags0064",2
objWSHShell.Run "C:\Windows\System32\cleanmgr.exe /sagerun:64",0,True
Hope it helps someone

• 已编辑 2012年5月10日 13:53
•
• 2012年5月10日 19:44

thanks Basty. So this will "check" the boxes for disk cleanup? So this is the registry location for making disk cleanup changes? Does this affect the last user logged in or all users?

thank you,

Brian

• 2012年6月6日 14:09

Hi Brian,

Sorry for the delay!  This is for Windows Server 2008 R2 (will also work on Win7), the above script will create a profile (number 64 in the above example), as it's in the HKLM registry it can be ran by any user with the appropraite rights from the command line.  It wont change the default profile you see in the GUI, I don't install the Desktop Experience feature on a server so you dont see the GUI unless you launch  cleanmgr.exe directly.

To change the default boxes checked for all users though you'll need to add a DWORD value to the appropraite registry key under: -

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches

Name = StateFlags

Value = 1

For example with the Recycle bin below: -

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin]
@="{5ef4af3a-f726-11d0-b8a2-00c04fc309a4}"
"StateFlags"=dword:00000001

Hope this is useful!

Ian

• 2012年6月14日 16:57

thanks Basty- I appreciate your help! I'll test this out and keep you posted.

Sincerely,

Brian