Automate Disk Clean up using vbscript

Could somebody please help me in automating disk cleanup of C drive using a VB script.

The option that I am aware of is as follows

1) cleanmgr.exe /d c: /sageset:11
2) the disk cleanup window will open up for me to choose what all to clear
3) After choosing the options, after I click okay, it will save my preference to registry
4) cleanmgr.exe /sagerun:11
5) running the above command will take the settings from registry and automatically start disk clean up without an user inputs.

I want to automate disk clean up on approximately 100 servers. It is not possible for me to follow the above solution since to implement that I will have to manually run the steps 1, 2 and 3 mentioned above on all 100 servers.

does anybody know how to automate the task entirely, ie without any manual efforts involved??

Any help is appreciated
ghelaniabhishekAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Do you know what and where the settings are stored in the registry?

If so, then it would become:
1) Import registry settings
2) Run cleanmgr.exe /sagerun:11

Regards,

Rob.
0
RobSampsonCommented:
There's info on the tool here:
http://support.microsoft.com/kb/315246

Regards,

Rob.
0
ghelaniabhishekAuthor Commented:
Thanks Rob,

but the question here is not about retrieving settings from registry. That is automatically taken care by sagerun. The problem here is setting the values in registry which as of now can be done using sageset attribute which again needs us to manually select the options we want. Which is not possible for me to do it over 100+ servers

 I suggest you try to run the below two commands, so that you will understand how it works.

cleanmgr.exe /d c: /sageset:11
cleanmgr.exe /sagerun:11
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

ghelaniabhishekAuthor Commented:
Rob, that url says exactly what I have mentioned. What I want is can I eleminate the step of manually setting up the options for disk cleanup??

 I suggest you try to run the below two commands, so that you will understand how it works.

cleanmgr.exe /d c: /sageset:11
cleanmgr.exe /sagerun:11
0
RobSampsonCommented:
OK, so I'll run sageset and see what it puts into the registry, then we can automatically insert those settings for sagerun to read.

Rob.
0
RobSampsonCommented:
OK, so it looks like, when you set a "job", it enumerates every key under
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches

and adds a DWORD value of
StateFlags####
where #### is the number you give the "job", then it sets that DWORD value to 2

Therefore, if you had a script that would set a DWORD value of 2 to a value name of StateFlags#### for options you want, and a value of 0 for options you don't want, then you should just be able to run
cleanmgr /sagerun:####

and it will pick up those settings.

So, I can knock up a script to put the required options on, for the required job number.....what number do you want, and what options do you want?

Regards,

Rob.
0
RobSampsonCommented:
0
ghelaniabhishekAuthor Commented:
number can be anything and options I want to select all the options available with disk cleanup and drive is C drive.

Downloaded Program Files
Temporary Internet Files
Recycle Bin
Temporary Files
Compress Old Files
Catalog files for the Content indexer
0
RobSampsonCommented:
OK, so here's the batch file version, job number 11:
@echo off
:: Set what we want to clear
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0011 /t REG_DWORD /d 0x02 /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files" /v StateFlags0011 /t REG_DWORD /d 0x02 /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin" /v StateFlags0011 /t REG_DWORD /d 0x02 /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" /v StateFlags0011 /t REG_DWORD /d 0x02 /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Compress Old Files" /v StateFlags0011 /t REG_DWORD /d 0x02 /f
REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Content Indexer Cleaner" /v StateFlags0011 /t REG_DWORD /d 0x02 /f

:: Clean it up
CleanMgr.exe /d C: /sagerun:0011

:: Remove our state flag
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0011 /f
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files" /v StateFlags0011 /f
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin" /v StateFlags0011 /f
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files" /v StateFlags0011 /f
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Compress Old Files" /v StateFlags0011 /f
REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Content Indexer Cleaner" /v StateFlags0011 /f

Open in new window


and here's the VBS version, job number 11:
Set objShell = CreateObject("WScript.Shell")
objShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files\StateFlags0011", 2, "REG_DWORD"
objShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files\StateFlags0011", 2, "REG_DWORD"
objShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin\StateFlags0011", 2, "REG_DWORD"
objShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files\StateFlags0011", 2, "REG_DWORD"
objShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Compress Old Files\StateFlags0011", 2, "REG_DWORD"
objShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Content Indexer Cleaner\StateFlags0011", 2, "REG_DWORD"

objShell.Run "CleanMgr.exe /d C: /sagerun:0011", 1, True

objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files\StateFlags0011"
objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files\StateFlags0011"
objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin\StateFlags0011"
objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files\StateFlags0011"
objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Compress Old Files\StateFlags0011"
objShell.RegDelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Content Indexer Cleaner\StateFlags0011"

Open in new window


In both cases, the flags are written to the registry, the job is run, then the flags are removed again.

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RobSampsonCommented:
Thanks for the grade.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.