Shrink and Clean Your Virtual Machines

I’m a rabid advocate of VMWare Workstation (and Server for that matter!). I maintain a separate virtual machine – and sometimes more than one – for each client I’ve worked with over the years. This has many advantages, including the ability to pull an old VM out of the archive for a returning client, the ability to install different versions of the portal to match the client’s configuration, and the capability to run different VPN software on the same host machine at the same time (because each VM maintains its own network stack and VPN software configuration).

But having 10-20 VMs can be a bit painful to keep up-to-date and to keep the file sizes down. While I still haven’t figured out how to apply all Windows Updates via command line, each of my VMs now has a batch file that allows me to compress and optimize disk space for each of the VMs that I have. So, once a month I log into my VMs, run Windows Update, and run the below batch file to shrink things up.

You can download the cleanup.bat file, or just copy/paste from the below code. Basically, the script performs the following activities:

  1. Delete the files in c:\windows\temp
  2. Delete the temporary Windows Update files in c:\ windows\ SoftwareDistribution\ Download\
  3. Delete the IE update files
  4. Run the Disk Cleanup Utility (Note: you need to do a one-time run of the command “cleanmgr /dc /sageset: 1” to set your prefs – see the Microsoft KB Article on the topic.
  5. Empty the recycle bin
  6. Defrag the drives
  7. Run the VMWare drive shrink utility
  8. Shut down the machine

So, do you have any optimizations that you think could be added to this script?

echo off

echo *** CLEANING FILES ***
if not exist c:\windows\temp goto NOTEMP
echo Cleaning Windows Temp...
cd c:\windows\temp
FOR %%A IN (*.*) DO DEL "%%A"
FOR /F "tokens=*" %%A IN ('DIR /B /AD') DO RMDIR /S /Q "%%A"

if not exist c:\WINDOWS\SoftwareDistribution\Download goto NODOWNLOAD
echo Cleaning Downloads...
cd c:\WINDOWS\SoftwareDistribution\Download
FOR %%A IN (*.*) DO DEL "%%A"
FOR /F "tokens=*" %%A IN ('DIR /B /AD') DO RMDIR /S /Q "%%A"

if not exist C:\WINDOWS\ie8updates goto NOIE8
echo Cleaning IE8 Updates...
cd C:\WINDOWS\ie8updates
FOR %%A IN (*.*) DO DEL "%%A"
FOR /F "tokens=*" %%A IN ('DIR /B /AD') DO RMDIR /S /Q "%%A"

if not exist C:\WINDOWS\ie7updates goto NOIE7
echo Cleaning IE7 Updates...
cd C:\WINDOWS\ie7updates
FOR %%A IN (*.*) DO DEL "%%A"
FOR /F "tokens=*" %%A IN ('DIR /B /AD') DO RMDIR /S /Q "%%A"

echo *** DISK CLEANUP ***
REM note: prior to using this batch file, you should run cleanmgr /dc /sageset: 1
REM       which will allow you to set your default options for the sage:1 profile
REM       this should also empty your recycle bin (or you could use "rd /s c:\recycler")
cleanmgr /dc /sagerun: 1

echo DEFRAG C:
defrag c: -f

if not exist D: goto NODDRIVE
echo DEFRAG D:
defrag d: -f

if not exist "C:\Program Files\VMware\VMware Tools" goto NOVMWARE
cd "C:\Program Files\VMware\VMware Tools\"
VMwareToolboxCmd.exe disk shrink c:\

if not exist D: goto NODDRIVESHRINK
VMwareToolboxCmd.exe disk shrink D:\

echo *** SHUTTING DOWN ***
shutdown -s -t 30

Tags: ,

Leave a Reply