I have looked all over the net for easy ways to resize VHDs. For those of you who don’t know what a VHD file is, it’s a Virtual Hard Drive. Now for years I have played with virtualization, normally with VMWare or Virtual Box (The latter being free) which has always dealt with some form of virtual hard drive, however the VHD (and now VHDX) has become a standard with virtualization, so much so that you can actually boot Windows off of a VHD without virtualization software. Both Windows 7 and 8 flavours of Windows support this feature.
So if you are like me, in charge of several systems, and you want to reimage them all, the best and easiest way is to do it via VHD. In my case we are using Dynamic VHDs, which means although you make the “Hard drive” 300GB, this is only a maximum size, the drive (file) will only be as large as the stuff you put on it… So if I have 30GB of OS and Software, my VHD File is only ~30GB and not 300GB. If you use a static disk, which has a slightly better performance (not by much), the drive (file) would be 300GB, which makes it harder to transport, and time consuming to transfer over from system to system.
The Best Practice
The best practice is to know your target a head of times, by this I mean know where your VHD is going, know what the physical hard drive limitations are, know what you are putting on it. Making the VHD the right size first time round, is the best practice.
Stuff Happens
That being said, stuff happens. For example, In my schooling instead of creating a VHD and installing an OS fresh from start (Server 2012) I opted to use the pre-installed evaluation VHD directly from Microsoft. This allowed me to spend more time updating than installing. As I have already done the installation several times, I decided I really didn’t need to do it another several times. The issue then of course is that you are at the mercy of Microsoft who decided 40GB was enough hard drive space to test with. They are not wrong, however if you are testing other stuff, like for instance Exchange Server 2013, then 40GB is not enough.
What about the reverse? This is the harder of the two, if you have to shrink the drive… One of the images I am creating for work is too big for another purpose I want to transpose it over to. This is probably one of the most sought after questions on the net, I know, I looked.
Method 1: If you have upgraded to WIndows 8(.1) Use a mixture of Hyper-V and DiskPart
Method 2: If you are still running Windows 7, Use Diskpart and VHDResizer
Note: The company that made VHDResizer no longer exists, it took me forever to find a clean version of the app, so instead of making you search for it, I gave you a direct link to my copy.
Enlarging the Disk and Volume
A. Enlarging the Drive
To enlarge the drive is very simple if you have Hyper-V installed. Note: you must have ALL of Hyper-V Installed, otherwise it won’t work, so if you don’t have the ability to install Hyper-V, skip to how to use Diskpart instead.
In Powershell (Not CMD) type the following Command:
1 |
PS C:> Resize-VHD –Path c:path-to-vhd-fileyour-VHD-file.vhd –SizeBytes 300GB |
This will enlarge to drive to 300GB, assuming the file was of course less than 300GB. You can also use TB for instance if you wanted to drive to be 1 Terabyte in size you would set the -SizeBytes to 1TB. ITs really that simple with he right tools.
You can also expand the Disk size in Diskpart (a little more lengthy, but great if you can;t install Hyper-V)
1 2 3 4 5 |
C:WindowsSystem32>diskpart DISKPART>Select vdisk file="c:path-to-vhd-fileyour-VHD-file.vhd" DISKPART>expand vdisk maximum=300000 |
B. Enlarging the Volume
This I usualy do in DiskPart, you can also do it from within Windows, however I would caution against it as working on the drive while the OS is in use may cause problems.
Lets start selecting the VHD and unlike expanding the Disk, we also have to attach the VHD to see the volumes.
1 2 3 4 5 |
C:WindowsSystem32>diskpart DISKPART>Select vdisk file="c:path-to-vhd-fileyour-VHD-file.vhd" DISKPART>attach vdisk |
Once we have attached the Vdisk, we need to take a look at the volumes to make sure we select the right one. If you don’t you might expand the wrong volume.
1 |
DISKPART>list volume |
You should see an output like this:
1 2 3 4 5 6 7 8 |
Volume ### Ltr Label Fs Type Size Status Info ---------- --- ----------- ----- ---------- ------- --------- -------- Volume 0 E DVD-ROM 0 B No Media Volume 1 F DVD-ROM 0 B No Media Volume 2 RECOVERY NTFS Partition 15 GB Healthy System Volume 3 C NTFS Partition 493 GB Healthy Boot Volume 4 D New Volume NTFS Partition 423 GB Healthy Volume 5 NTFS Partition 149 GB Healthy |
Select the volume that corresponds with the Volume you wish to enlarge and use the extend command to fill the empty space
1 2 3 |
DISKPART>select volume 5 DISKPART>extend |
Doing the “List Volume” command again you will see that the partition has grown to the full size of the drive (minus a bit)
1 2 3 4 5 6 7 8 |
Volume ### Ltr Label Fs Type Size Status Info ---------- --- ----------- ----- ---------- ------- --------- -------- Volume 0 E DVD-ROM 0 B No Media Volume 1 F DVD-ROM 0 B No Media Volume 2 RECOVERY NTFS Partition 15 GB Healthy System Volume 3 C NTFS Partition 493 GB Healthy Boot Volume 4 D New Volume NTFS Partition 423 GB Healthy Volume 5 NTFS Partition 299 GB Healthy |
If you only wish to extend it a bit and not the full size you can use the size switch, such as the following code would make it 200GB:
1 |
DISKPART> extend size=200000 |
Voila, your Drive and Volume are extended. If you chose to not extend the volume, you can make another partition in Diskpart, or use Windows.
Shrinking Disk and Volume
A. Shrinking the Volume
Again this can be done in Windows, by attaching the VHD in Disk Management, right clicking on the volume and selecting “Shrink”, this is the easiest way, however if you are shrinking the partition of OS you are in, might cause some problems.
To do it in Disk Part once you have the Volume selected, you can use the Srink command. FIrst you want to find out by how much you can shrink it:
1 2 3 |
DISKPART>shrink querymax The maximum number of reclaimable bytes is: 48 GB (49697 MB) |
It will return the most you can shrink it by using the desired switch
1 |
DISKPART>shrink desired=40000 |
The above would remove 40 GB from the volume making to 40GB less in size.
B. Shrink the Drive
There is no easy way to shrink a drive natively on Windows UNLESS you can install Hyper-V. I have search and searched and found that Diskpart seems to be missing the proper commands. Compact simply reduced the file size of a dynamic disk if you have removed some files from it, as once a dynamic drive increases, it does not automatically decrease when files are removed. Shrink is used only to shrink a volume, which amazingly gets confused a lot online as people seems to think the Volume is the same thing as the VHD, which is it not. A Volume is a partition on a drive, the VHD is the Drive itself.
In Hyper-V you would simplu run the same command you did to extend:
1 |
PS C:> Resize-VHD –Path c:path-to-vhd-fileyour-VHD-file.vhd –SizeBytes 300GB |
Assuming the size of course is smaller than the original.
If you cannot install Hyper-V, then you can shrink the volume down using VHDResizer however this process is length depending on how much stuff you actually have on the drive.
I speant almost 3 hours scouring for answers to this and your last paragraph summed it up and it worked perfectly. Easiest way to shrink a VHDX file is to simply install HyperV on a server, drop the VHDX file on it, or connect to it by mapped drive, use the EDIT Disk function of Hyper V and Voila. DONE
Good job.
Cassidy
B. Shrink the Drive <<<<<<<<<<
There is no easy way to shrink a drive natively on Windows UNLESS you can install Hyper-V. I have search and searched and found that Diskpart seems to be missing the proper commands. Compact simply reduced the file size of a dynamic disk if you have removed some files from it, as once a dynamic drive increases, it does not automatically decrease when files are removed. Shrink is used only to shrink a volume, which amazingly gets confused a lot online as people seems to think the Volume is the same thing as the VHD, which is it not. A Volume is a partition on a drive, the VHD is the Drive itself.
I must say this was a great help for my trying resize a few Virtual drives on server. So thanks..
***Can you please share the name of the terminal you use to illustrate the commands her…?
I wish to use multi colors like you did here…
Hello Ismael, glad you found my tutorial helpful. As for your request, those are not screen shots of a terminal, but rather a plugin for the site which colors code snipets automatically.