If you've setup User Profile Disks with your remote desktop deployment, what I'm about to show you will be a life saver. I grappled with this for a while trying to find the best method to both Compact and Expand a VHDX file. This will work with any VHD or VHDX file irrespective of whether it's a User Profile Disk.
This is especially pertinent when you're working under a virtual environment other than Hyper-V like VMWare, KVM and Xen because it saves you having to install the Hyper-V role to get the VHDX administration tools. Historically it was not a good idea to nest another Hypervisor while running under the host Hypervisor. Not sure if that's still the case, but better to be safe than sorry.
Let's get started:
We're going to be utilising DiskPart which is the Disk Partition utility built into all versions of Windows since Windows Server 2003.
To Compact a VHDX file, open an admin command prompt and input the following:
DiskPart
Select vdisk file="D:\TS-Profiles\YourVHDX-File.vhdx"
attach vdisk readonly
Compact vdisk
detach vdisk
exit
The Select vdisk file option is the path to the VHDX file you wish to compact including the file name. Once done that's it, nothing further to do.
To Expand a VHDX file, open an admin command prompt and input the following:
DiskPart
Select vdisk file="D:\TS-Profiles\YourVHDX-File.vhdx"
Expand vdisk maximum=30720
detach vdisk
exit
In the example above I've expanded the disk to 30GB which, doing the math, is 1024*30 which equals 30720. Change this value to suit your requirements. The Select vdisk file option is the path to the VHDX file you wish to expand including the file name.
If you've found this useful, you may want to sign up to our newsletter where you'll receive notices on when we post new articles and helpful "how tos". Just fill out your details below and we'll do the rest...