Monthly Archives: May 2012

Defrag VM disk automatically with powershell

Finally got some time to implement automatic disk defrag and repair for VMware workstation virtual disks. I think I’ve spent more time thinking about it that actually writing the script.

 

$exePath = “C:\Program Files (x86)\VMware\VMware Workstation\vmware-vdiskmanager.exe”

$VMPath = “D:\Virtual Machines”

ls -Path $VMPath -Recurse -Filter *.vmdk | %{ .$exePath -R “”$_.FullName””} | Out-Null

ls -Path $VMPath -Recurse -Filter *.vmdk | %{ .$exePath -d “”$_.FullName””} | Out-Null

 

Log file will be automatically created @ “%temp%\vmware-%username%\vdiskmanager.log”

Run it as scheduled task: powershell.exe -WindowStyle Hidden C:\defraghdd.ps1

Magic!