A way to automate clean up of Windows XP or Vista workstations. This is one way, other possibilities are endless…
The script and initial set up will run the cleanup manager, clean up any temporary stuff the cleanup manager may miss, and then defragment all hard drives if needed. First, the script is presented and in the following steps things will be explained a bit more in detail.
Step 1: Create the VB Script
You will want to open a text editor and copy and paste the following script into the editor. When complete, save the file with your choice of file name and be sure to use the .vbs extension.
Copy from line to line:
'____________________________
Option Explicit
’ This has been tested on Windows XP Professional & Vista and works.
’ Clean up script.
’ Before you ever run this script execute
’ cleanmgr.exe /sageset:100
’ and choose the options for the clean up
’ The sageset option will store options for the cleanmgr program
’ in the registry. The chosen options are retrieved and used
’ when the sagerun option is invoked.
Dim WshShell, fso, d, dc, Return, sStatement, counter, arrCmd(5), strComputer, objWMIService, colOperatingSystems, objOperatingSystem
strComputer = “.”
Set WshShell = WScript.CreateObject(“WScript.Shell”)
’ This will use the clean up manager to clean stuff as mentioned above.
WshShell.Run “cleanmgr.exe /sagerun:100”, 1, TRUE
’ This array of commands will clean up what the clean up manager misses.
arrCmd(0) = “cmd /c rd /s /q %temp%”
arrCmd(1) = “cmd /c if not exist %temp%\nul md %temp%”
arrCmd(2) = “cmd /c rd /s /q %windir%\temp”
arrCmd(3) = “cmd /c if not exist %windir%\temp\nul md %windir%\temp”
arrCmd(4) = “cmd /c rd /s /q %windir%\Prefetch”
arrCmd(5) = “cmd /c if not exist %windir%\Prefetch\nul md %windir%\Prefetch”
’ Run through the array of commands
For counter = 0 to 5
sStatement = arrCmd(counter)
WshShell.Run sStatement, 7, TRUE
Next
Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\” & strComputer & “\root\cimv2”)
Set colOperatingSystems = objWMIService.ExecQuery (“Select * from Win32_OperatingSystem”)
For Each objOperatingSystem in colOperatingSystems
If objOperatingSystem.Caption = “Microsoft Windows XP Professional” Then
’ Now we defrag all hard drives.
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set dc = fso.Drives
For Each d in dc
If d.DriveType = 2 Then
Return = WshShell.Run(“defrag " & d & " -f”, 1, TRUE)
End If
Next
End If
Next
Set WshShell = Nothing
WScript.Quit
'-------------------------------------------
Step 2: An explination of the script…
As you read the comments in the VB script you will notice things have been explained there (somewhat) already. This explanation is really for those who want to review this before taking the steps to implement:
First, before ever running the script you will want to invoke the cleanup manager and set some default options to it. To do this click Start => Run or within a command prompt enter the command:
cleanmgr.exe /sageset:100
The ‘sageset’ argument will store options for the cleanup manager in the registry. The number argument is a sort of index for those options, so, theoretically you can have many different configurations saved within the registry each with it’s own index number.
The first command invoked within the script is the cleanup manager.
cleanmgr.exe /sagerun:100
Notice the ‘sagerun’ argument? This invokes the cleanup manager with the options set for index 100 in the registry saved with the ‘sageset’ option.
The next portion of the script is an array of commands that will remove and recreate temporary directories and also the Windows Prefetch directory. A machine can slow to a crawl with enough temporary stuff lying around the hard disk that has to be read every logon/reboot. For this reason and the fact that the shop where I work has some older, slower equipment I have found it to be a blessing to keep this cleaned up as much as possible. If you are so inclined you can skip this portion of the script by commenting or cutting that section of the script out. If you do so, make sure you disable/remove the array definition and the following loop:
++++++++++++++++++++++++++++++++++
’ This array of commands will clean up what the clean up manager misses.
arrCmd(0) = “cmd /c rd /s /q %temp%”
arrCmd(1) = “cmd /c if not exist %temp%\nul md %temp%”
arrCmd(2) = “cmd /c rd /s /q %windir%\temp”
arrCmd(3) = “cmd /c if not exist %windir%\temp\nul md %windir%\temp”
arrCmd(4) = “cmd /c rd /s /q %windir%\Prefetch”
arrCmd(5) = “cmd /c if not exist %windir%\Prefetch\nul md %windir%\Prefetch”
’ Run through the array of commands
For counter = 0 to 5
sStatement = arrCmd(counter)
WshShell.Run sStatement, 7, TRUE
Next
++++++++++++++++++++++++++++
The next and final portion of this script is a modification of a defragment script found on the Microsoft MVP site of Doug Knox. The script uses the WMI Service to find which operating system is in use and decides to run the defragment command based upon the result of the query. For Windows Vista, defragment is automatically scheduled by default but is not so for Windows XP.
That’s the script, in a nutshell…
Step 3: Add a sceduled task
To keep from having to run this manually, I set this script up as a scheduled task. This is a very basic step and I will leave that to you. For me, it worked best to schedule the script in the middle of the night while the office is, for the most part, closed. The options are always yours…
There are endless ways to do your jobs. The one point I have seen throughout the SpiceWorks community thus far is the advice to automate as much as possible. Scripting or creating batch files to make your job easier is a given that everyone should entertain if not doing so already. The material I have presented here is just the way I have chosen to deal with clean up. Modifications and other ideas should always be explored.