Optimize VMware Virtual Machines Using a Batch File

Years ago, there was a couple of batch files that were floating around the Internet that were designed to defrag and shrink virtual machines created and used by VMware Workstation or VMware Server. Over time, the scripts became a little obsolete in that some of the command line functionality was lost from the removal of a utility, vmware-mount, from newer versions of VMware Workstation. That utility among others is now part of the VMware Virtual Disk Development Kit. With the installation of the development kit and a rewrite of the scripts into one, here is an updated script that works.

To get started, the VMware Virtual Disk Development Kit must be downloaded and installed. Do a search with your favorite search engine for VMware-vix-disklib-1.1.1-207031.i386.exe to find the kit. It is a free kit offered by VMware.

The virtual machine is defragmented. Using utilities from the development kit, the script is will map a virtual drive. The script uses drive m, this can be changed to a drive letter more suitable to your environment. This mounted drive is then run through a process to shrink any available space, possibly saving GB’s of hard drive space.

It is worth noting that these utilities will NOT work if the virtual machine contains a snapshot. These will need to be deleted at your own risk.

Here is the updated script.

:: Requires: VMware-vix-disklib-1.1.1-207031.i386.exe
@echo off >nul
echo VMware Virtual Disk Optimizer - build 2013.0604.
echo.
set vm_bin="C:\Program Files (x86)\VMware\VMware Virtual Disk Development Kit\bin"
if exist ????????.log (
	echo Deleting logs...
	del /s /q *.log
	)
if exist caches (
	echo Deleting caches...
	rd /s /q caches
	)	

for /r %%i in (*.vmdk) do call:shrink_disk "%%i"
echo Done!
pause
goto:eof

:shrink_disk
:: Defragment the specified virtual disk. Local only.
echo Defragmenting %1
echo.
%vm_bin%\vmware-vdiskmanager -d %1

echo.

:: Shrink the specified virtual disk. Local only.
echo Shrinking %1
echo.
%vm_bin%\vmware-vdiskmanager -k %1
goto:eof

:eof
exit[/code]

Here is the original script.

:: Requires: VMware-vix-disklib-1.1.1-207031.i386.exe
set vm_bin="C:\Program Files\VMware\VMware Virtual Disk Development Kit\bin"

del /s /q *.log
for /r %%i in (*.vmdk) do call:shrink_disk "%%i"
echo Done!
pause
goto:eof

:shrink_disk
:: Deletes the mapping to a virtual drive volume.
%vm_bin%\vmware-mount m: /d
:: Defragment the specified virtual disk. Local only.
%vm_bin%\vmware-vdiskmanager -d %1
:: Creates the mapping to a virtual drive volume.
%vm_bin%\vmware-mount m: %1
:: Prepare the mounted virtual disk specified by the drive-letter for shrinking.
%vm_bin%\vmware-vdiskmanager -p M:
:: Deletes the mapping to a virtual drive volume.
%vm_bin%\vmware-mount m: /d
:: Shrink the specified virtual disk. Local only.
%vm_bin%\vmware-vdiskmanager -k %1
goto:eof

:eof
exit

To use the script, place the script in the folder where the virtual machine resides. Shutdown the VMware Workstation or VMware Server so that it is not in use. Then execute the script.