Skip to main content

Windows maintenance script

I got tired of running maintenance tasks like Disk Cleanup and Disk Defragmenter on Windows machines, so I wrote a simple maintenance script called maint.vbs.

Before running maint.vbs, run sageset.cmd to save your desired Disk Cleanup options to the Registry so you only need to choose what Disk Cleanup does once. I check everything except Recycle Bin. To save this as a command file (similar to a DOS batch file), paste the code below into a new text file, and save it as sageset.cmd.

@ECHO OFF
ECHO Only run this once, after a fresh Windows install.
ECHO Recommended selections are all except Recycle Bin
REM allows user to choose Disk Cleanup options, then saves them to the Registry to be called by sagerun in maint.cmd
PAUSE
cleanmgr.exe /d c: sageset:0
EXIT

Now you can run maint.vbs. This script does the following:
  • Checks for Microsoft Updates.
  • Runs Disk Cleanup, using the options you specified when you ran sageset.cmd.
  • Checks C: drive fragmentation, and defragments it if Windows determines that it's necessary.
  • Backs up files using Robocopy.
  • Scans C: for viruses with Avira AntiVir (must be installed).
  • Writes the following to a log file, maint.log: computer hostname, time maint.vbs completed, whether C: was defragmented, and the elapsed execution time for maint.vbs.
  • Opens a dialog box telling the user that maintenance is complete.
To save this as a VBScript file, paste the code below into a new text file, and save it as maint.vbs.

'starts timing script execution
dtmStart = Now

Set WshShell = CreateObject("Wscript.Shell")

'polls for Microsoft Updates
WshShell.Run "wuauclt.exe /detectnow", 1, true

'runs Disk Cleanup based on choices in sageset
WshShell.Run "cleanmgr.exe /sagerun:1", 1, true

'runs Disk Defragmenter analysis
Set WshExec = WshShell.Exec("defrag.exe c: /a")
'memorizes and reads analysis
strOutput = WshExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume."
'an analysis other than "You do not need to defragment this volume" causes a defrag
strDefragStatus = "no"
If InStr(1, strOutput, strSearch) = 0 Then
WshShell.Run "defrag.exe c:", 1, true
strDefragStatus = "yes"
End If

'backs up My Documents if computer hostname matches specified one, else skips
'
'If strComputername = "hostname" Then
'uses robocopy to back up documents
'WshShell.Run "i:\apps\scripts\robocopy.exe ""%userprofile%\My Documents"" ""i:\%computername%\My Documents"" /fft /s /v /purge", 1, true
'uses robocopy to back up desktop
'WshShell.Run "i:\apps\scripts\robocopy.exe ""%userprofile%\Desktop"" ""i:\%computername%\Desktop"" /fft /s /v /purge", 1, true
'End If

'scans C: with antivirus
WshShell.Run """%programfiles%\Avira\AntiVir PersonalEdition Classic\avscan.exe"" /GUIMODE=1 /PATH=""c:""", 1, true

'stops timing script execution, memorizes time in minutes
dtmEnd = Now
dtmExecutionTime = DateDiff("n", dtmStart, dtmEnd)

'records success to file and tells user script is complete
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForAppending = 8
Set objTextFile = objFSO.OpenTextFile("\\istanbul\public\apps\scripts\maint.log", ForAppending, True)
Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputername = WshNetwork.ComputerName
objTextFile.WriteLine(strComputername & vbTab & Now & vbTab & strDefragStatus & vbTab & dtmExecutionTime)
objTextFile.Close

Wscript.Echo "Maintenance completed at " & Time & vbCrLf & "Elapsed time: " & dtmExecutionTime & " minutes" & vbCrLf & "Defrag: " & strDefragStatus

To get maint.vbs to run from a network drive in Windows Vista, I have to call it from this maintvista.vbs script:

Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = "\\istanbul\public\apps\scripts\maint.vbs"
If FSO.FileExists(strPath) Then
objShell.ShellExecute "wscript.exe", Chr(34) & strPath & Chr(34), "", "runas", 1
Else
Wscript.Echo strPath & " not found!"
End If

To learn more about VBScript, check out the VBScript Tutorial at W3Schools.

Comments

Popular posts from this blog

The difference between burritos, chimichangas, and enchiladas

I love Mexican food, but I'm embarrassed to admit that I always get confused between burritos, wet burritos, chimichangas, and enchiladas. Here are the descriptions, with the differences in bold and pictures following each description.

burrito
A flour tortilla wrapped around a filling (meat, beans, vegetables, etc)


wet burrito
A burrito that's covered in red chili sauce and cheese. Because of the sauce covering, it looks like an enchilada, but it's made with a flour tortilla, whereas the enchilada is made with a corn tortilla.


chimichanga
A burrito that's deep-fried. Sometimes covered with cheese or another topping.


enchilada
A corn tortilla wrapped around a filling, covered with chili pepper sauce


Sources
Wikipedia: BurritoWikipedia: ChimichangaWikipedia: Enchilada

Edit scanned documents with Word 2007

Office 2007 includes support for converting scanned documents to editable text using OCR (optical character recognition). To get your text from a paper document to Word 2007:
In the Control Panel, open Add or Remove Programs.Find Microsoft Office, click it, and click Change.In the Office Installation Options window, expand Office Tools, click Microsoft Office Document Imaging, and select Run from My Computer from the dropdown.Click Continue or Next until you reach the end.You can now scan documents and convert the scanned images to editable text:
From the Start Menu, find Microsoft Office, then select Microsoft Office Tools, then click Microsoft Office Document Scanning.Choose your preset and options, then click Scan.The scanned image should open in Microsoft Office Document Imaging. To perform OCR and open the editable text in Word, click Tools, Send Text to Word.You can now edit and save the scanned document as a Word document.

Considering retiring this blog

I'm seriously considering retiring this blog. When I started it in 2008, it was a place for me to share thoughts and links on a variety of topics. For at least the last year, however, it's just been a place where I re-post book reviews from Goodreads.

You can see all my book reviews on my Goodreads profile, and subscribe to my Goodreads RSS feed to be notified of new reviews. I also intend to keep blogging about WordPress, web design, and web-related topics on my OptimWise blog, which also has an RSS feed.

Would you miss this blog if I discontinued it? Please leave a comment.