5 Ways To Clean Up Your Computer With An Automated Script [Windows]

Ryan Dube 26-10-2011

automate computer cleanupComputer maintenance isn’t exactly something that most people remember to do. For the general user that isn’t exactly tech savvy, when a computer doesn’t work, they either call for help or simply buy a new computer.


If you have family members or friends that are always calling you for help with a slow computer, I’m going to offer you a solution that you can use to stop those phone calls right now. This solution incorporates six tools into a Windows script. That script is going to perform all of the PC cleanup work that you would do if you were sitting right in front of the computer yourself.

What This Script Will Accomplish

This Windows script is going to run the required tools in command line mode. Many of these tools that you’ve been using for years, you may not have even realized that there is a command-line mode available.

Clean Up Registry & Temp Folders

The first step is probably the most important. We’re going to launch CCleaner in command line mode to clean up temp files and the registry.

One catch. In order to make this script work without constant notifications about the apps making computer changes, you’ll have to disable the User Account Control windows feature in Windows 7.

5 Ways To Clean Up Your Computer With An Automated Script [Windows] autoclean1


Now you’re ready to roll. Create a file called CleanComputer.wsf and start it off with the following script.

<script language="VBScript">
Option Explicit
On Error Resume Next
Dim WshShell
Dim retVal
set WshShell=CreateObject("WScript.Shell") "CCleaner.exe /AUTO"

Danny covered CCleaner Optimize Your System To Run At Its Best With CCleaner Over the last two years, CCleaner has changed quite a bit in terms of version numbers...up now to version 3.10 at the time of this writing. While visually the program actually hasn't changed much (it's... Read More recently, so you can see just what it is capable of and how much it can optimize your system. When you launch it in the script as shown above with the /AUTO flag, it’ll run invisible and it’ll use the settings you used when you last ran the application. You’ll see CCleaner.exe running in the Task Manager.

automate computer cleanup

If you configure CCleaner beforehand, in auto mode it’ll automatically clean up temporary files, log files and your registry as well.


Clean Up Spyware

Next up is to take care of any spyware that might be running on the PC. My favorite tool for that is Spybot, and luckily Spybot also offers a command line feature. The one problem here is that the path to Spybot is full of spaces, which is hard to handle in a Windows Script shell command.

So, create a .bat file with the following:

C:\Program Files (x86)\Spybot - Search & Destroy\SpybotSD.exe" /taskbarhide /autocheck /autofix /autoclose

Save it as SpyBot.bat in the same directory as your Windows script. Then in the next line of your WSF file, add the following: "spybot.bat"

Your Windows Script will launch your batch job that will launch Spybot in command-line mode. You will know it’s running when the Exe appears in the task manager.


script to clean temp files

Those four parameters after the spybotsd.exe file  will run Spybot in silent mode, automatically remove any spyware found, and then automatically close.

Remove Malicious Software

In addition to anything Spybot catches, I also like to run the Microsoft Malicious Software removal tool. Download the executable from the “Microsoft Download Center” section, save it in your script directory as “malremove.exe” and then add the following line to your growing Windows Script. "malremove.exe /Q /F:Y"

The /Q command tells the malware removal tool to run in quiet mode (no interface) and /F:Y tells it to forcibly remove any malware that it finds without any intervention required. The first time you run it during your initial testing, you’ll have to select the option for no warning the next time it runs.


script to clean temp files

Here it is running in the background.

script to clean temp files

So, we’ve covered most of what you’d probably do if you were there in person – run CCleaner to get rid of temporary Internet files and other temp files, remove spyware and malware, so what’s left?

Automate Disk Cleanup

Another common tool that can help computer performance is the Windows Disk Cleanup tool.  First you have to set it up. Go to Start -> Run and type: “cleanmgr /sageset:1“. The following window will appear.

computer clean up script

Set up the items that you’d like your command line to automatically clean, and then click OK. This tells it that whenever you choose /sageset:1, to utilize the settings you’ve just created. Now, in your Windows Script, add the following line. "Cleanmgr /sagerun:1"

This will run the Windows disk cleanup silently, using the settings you’ve just created. You can also install the latest Windows updates by first installing WuInstall.exe and adding this line. "wuinstall.exe /install /reboot_if_needed"

And don’t forget to do a quick defrag of the hard drive by adding the following lines to the script. “Defrag volume c:” "Defrag volume d:"

Automate Virus Scan After Cleanup

Last, but definitely not least, you’ll also want to end your script by firing off a full virus scan. For example, I use Kaspersky, which offers its own set of command line parameters.

In my case, I just add the following final line to my script. "AVP.exe SCAN /ALL /i4"

And that kicks off the command window and runs through a full scan (which can take several hours).

automate computer cleanup

Most other anti-virus providers out there offer similar command-line options.

So, once you’ve added all of these lines, close out your Windows Script.


Once you’ve finished the procedure above and saved your file on the PC, just schedule the job to run on a regular basis (/Accessories/System Tools/Task Scheduler). Give the procedure above a try and see how well it works for you.

Did it work well, and can you think of any other useful command line tasks that could also be added? Share your thoughts in the comments section below.

Image Credit: Shutterstock

Related topics: Computer Automation, Computer Maintenance, Programming.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Swami
    January 25, 2018 at 10:42 am

    Can we add Malware Bytes also to the script? What should I add to the script file to execute this?

  2. RazorFlight
    August 18, 2016 at 5:11 pm

    CCleaner can only be configured to clean junk in auto mode there is no way to automate the registry cleaning process or any other CCleaner tools. Also simple batch script
    "CCleaner Portable516\CCleaner64.exe" /AUTO" is much easier :)

  3. Isaac
    November 25, 2011 at 4:46 am

    so i keep getting a WSH error message saying "Expected ';'" in line 4 col 9.  what am i doing wrong?

    • Ryan Dube
      November 25, 2011 at 4:45 pm

      Hi Isaac, I'm not sure without seeing your code, but try to make sure that all your quotes "" are closed off properly. 

  4. mdawu
    November 3, 2011 at 10:12 am

    yeah its good

  5. Ramas
    November 1, 2011 at 8:07 pm

    Why do i get this error with ccleaner?

    • Rushnosh
      November 4, 2011 at 6:06 am

      Not too sure if this helps, but can you go to Start>Run and then type up ccleaner.exe to see if this pops up as normal?
      Sometimes you'll need to be in the same directory for as of CCleaner.
      Try using the same method with Spybot and run the batch script pointing to the part:

      Script: "CCleaner.bat"

      ::Configure this line to where CClener is installed
      C:Program FilesCCleanerCCleaner.exe /AUTOexit???The script may not be able to compile properly also, you made sure you copyed everything from this post to the clear.wsf?

    • Ryan Dube
      November 5, 2011 at 2:47 pm

      Ramas - what does your code look like at line 11?

  6. Seti
    November 1, 2011 at 9:10 am

    I added:
    Ipconfig /flushdns
    bleachbit --clean --preset (once I setup what I wanted cleaned manually)
    speedyfox /default_profile (clean firefox tracks)
    RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255 (Clean IE tracks)

    Great article...helped me move from a batch script to something better...thanks!

    • Rushnosh
      November 4, 2011 at 6:22 am

      Something simple but I would usually perform this when I go to bed...
      So I added:

      shutdown /s

      But thanks for the other tips Seti :)

      • Rushnosh
        November 4, 2011 at 6:33 am

        Oops ... Minus the "ipconfig /flushdns"

        Not much point for this function for me since Im shutting down the computer ... hehe

        bleachbit --clean --preset (once I setup what I wanted cleaned manually)??

        Never heard/used it... Just readup and its an Internet Browser cleaner, will give it try :D

        • Ryan Dube
          November 5, 2011 at 2:47 pm

          Very cool tips you guys - thank you. I've added these to mine as well.

  7. Beaks
    October 30, 2011 at 10:09 pm

    It all sounds wonderful. I only wish I knew how to start or set up a script

    • Ryan Dube
      October 30, 2011 at 11:47 pm

      Hey Beaks - you just write the script as shown in Notepad and save it as a .wsf file. When you double click the file, it'll run your script. You don't need any special programs or compilers - that's the beauty of Windows scripting. Give it a shot, you might surprise yourself!

  8. Fu Man Chu
    October 27, 2011 at 12:30 pm

    Things like CCleaner, Spybot, etc. constantly update themselves though.. sometimes to whole new versions... I guess those apps will still have to be clicked on individually every now and then to update+configure no? That's the only catch I can think of to this thing but other than that GREAT to know about how to automate via scripts.

    • Ryan Dube
      October 28, 2011 at 3:34 am

      Fu Man Chu - absolutely. It's still a very good idea to manually update those apps with the latest updates/definitions every now and then. Very good point.

    • JT Baugh
      November 16, 2011 at 4:14 pm

      Hey Fu. I am super burned out right now from a long night of updates, so I don't know if someone already posted this but I actually found a workaround for that. Most of the programs have the ability to set the preferences via an .ini file. So what I did was add the ini files for CCleaner and Defraggler in my updates folder and before the programs are called I use the xcopy function to copy the ini files into the ccleaner and defraggler program folders respectively. If that isn't clear let me know and I'll post my code. I am too tired right now to dig through and find it LOL GL 

      • Tina
        November 17, 2011 at 6:06 pm

        Thanks for sharing your solution, JT!

  9. Arun Kumar
    October 27, 2011 at 7:11 am

    Awesome script compilation Ryan Thanks a million, You eased my work to lot of extent.
    Few points from me:
    1. I use Microsoft Security essential free Antivirus tool here is its details on how to use it with command line I am using "MPCMDRUN.EXE SCAN 0" I hope that is correct?
    2. How do I make this whole script run automatically in background when I am watching movies, is it possible to do the same using task scheduler? or any other tool.
    3.  I keep on installing lot of programs and then forget to uninstall it if I am not using it, is it possible to put auto uninstaller if program is not used for 3 to 6 months.

    • Ryan Dube
      October 28, 2011 at 3:33 am

      Thanks Arun and thanks for the command line for Microsoft Security essential - that's awesome. Yes, you can schedule the Windows script to run using Windows scheduler - it'll launch on its own and you don't have to do a thing. #3 is a really cool idea Arun...I will research that one and maybe do an article on it if it's possible. Thank you!

  10. Rushnosh
    October 27, 2011 at 3:23 am

    Great article :)
    I have a different anti-virus system (Trend Micro Titanium Internet Security 2011). Will investigate what command line options this have. If anyone used this over command line before please share.Otherwise thanks for the code. Will give it ago.

    • Ryan Dube
      October 28, 2011 at 3:32 am

      Awesome - glad it's helpful. Good luck with the command line codes for Trend Micro. Wish I could help there but I've never used it.

      • Rushnosh
        November 4, 2011 at 6:23 am

        I still have not found a way as of yet. :( .... Been reading about Safe mode commandline features but nothing to do with Titanium Internet Security 2011 :( .... Ahhh well ...

        • Ryan Dube
          November 5, 2011 at 2:46 pm

          Yeah - unfortunately not all antivirus software packages offer a command line mode. You might consider installing one of the free antivirus packages we've suggested here at MUO, and use that one as part of your script?

  11. Dom
    October 26, 2011 at 10:35 pm

    Thanks for this post - I've got a script on a USB I use when cleaning people's computers but will add some of the items you have used here to it!

    Thanks a lot.
    Really good article

    • Ryan Dube
      November 5, 2011 at 2:45 pm

      Cool - thanks Dom. Having a USB loaded with such a script is a great idea.

    • Jtaylor
      November 25, 2011 at 11:24 am

      Hi Dom, could you please share me some light on your Script that you are using or would you be Kind to share it with me please ?