Expanding a VM’s hard drive using Powershell

Recently we needed to expand our Virtual Desktop VMs from 10GB C: drives to 15GB to accommodate some updates for one of our primary applications. The VMs are chronically low on free space so the decision was made to expand the drive.

The first step was to grow each VM’s VMDK from 10GB to 15GB. This was easily accomplished with Powershell:

Get-Folder Desktops | Get-VM | Get-HardDisk | Where {$_.CapacityKB -eq 10485760} | Set-HardDisk -CapacityKB 15728640 -Confirm:$false

Simply put, this one-liner acts against all VMs in the “Desktops” folder, and if it currently has a 10GB drive (10,485,760 KB), it uses the Set-HardDisk command to expand it to 15GB (15,728,640 KB). This worked incredibly well. I did run in to one or two VMs that had trouble expanding because they could not be “stunned” while currently in the process of VMotioning to another host. They weren’t actually migrating, so I quickly powercycled them and ran the one-liner again, with success.

The VMDKs are thin-provisioned so no additional space was consumed on the datastores, but now the disks can grow beyond 10GB, up to 15GB.

The next step which fortunately is “not my problem” is to expand the C: partition from 10GB to 15GB. I advised our desktop manager to try Dell’s “extpart” utility which can expand the C: partition while the VM is booted. I have used it successfully with Windows Server 2003, but not tried it with XP. I believe it will work though. Our desktop manager will probably make use of Altiris and some batch scripts to run extpart on all the desktops.

7 thoughts on “Expanding a VM’s hard drive using Powershell

  1. Pingback: Tweets that mention VirtIRL · Expanding a VM’s hard drive using Powershell -- Topsy.com

  2. Tomas

    Diskpart (built in in windows) can expand the disk while the system is powered on. Basically you do “Select disk 1” “Select partition 1” “Extend” and you’re done.

    1. chouse Post author

      That is true, but in XP, you cannot use diskpart to extend the C: drive, but you can use Dell’s extpart.

  3. Ken

    I have expanded hundreds of virtual disks with very few issues using clonezilla. Just mount the VM’s CD drive to the clonezilla ISO and boot it. There were times when this corrupted some disks but I never figured out what caused it and it hasn’t happened in a long while.

    This works on XP, and every other version of Windows I use.

    1. chouse Post author

      Sure, Clonezilla or Gparted would work fine – but they require a reboot. Powershell to expand the VMDK and extpart to expand the partition all allow growing the system drive while the VM is running. No reboot required.

  4. BenB

    Have tried running this, converting a Win XP VM from 15GB to 20GB but I get an error:
    C:\>Get-VM VMTEST | Get-HardDisk | Set-HardDisk -CapacityKB 20971520 -Confirm:$false
    “Set-HardDisk Length cannot be less than zero.”
    I also can’t increase in the vSphere Clinet GUI, the disk provision is greyed out. The VM has no snapshots, it’s powered off. So not sure why!

    1. adam

      There may not be a snapshot but you may have to consolidate the VM.. Instead of going into the snapshot manager when you right click on the VM click Consolidate instead..

Comments are closed.