Enlarge virtual hard disks in Hyper-V with PowerShell

I would also like to start by thanking Frank for allowing me to contribute to FrankysWeb with my own articles. As I work a lot with Hyper-V servers, I would like to share my knowledge here and write some articles about it. I would like to start with instructions on how to increase or expand virtual hard disks in Hyper-V servers.

Differences in virtual hard disks

Before the enlargement is carried out, you should call up the settings of the virtual machine. There you can check whether the hard disk in question is connected via IDE controller or SCSI controller. This determines whether the expansion is possible during operation or whether you have to shut down the VM first.

While virtual IDE hard disks cannot be managed during operation, most operations on SCSI hard disks are also available during execution (at least from Windows Server 2012R2 or Windows 8.1). Whether the format of the hard disk is a VHD or a VHDX file or whether the VM was created as 1st generation or 2nd generation does not matter.

Enlarge virtual hard disk

You can enlarge a virtual hard disk either via the Hyper-V Manager or (faster) via PowerShell. In the Hyper-V Manager, you must select the relevant hard disk in the VM settings and then start the wizard for editing virtual hard disks using the "Edit" option. This will then guide you through the next steps.

Enlarge virtual hard disks in Hyper-V
A wizard for expanding virtual disks can be called up via the Hyper-V Manager. However, it is quicker to use PowerShell.

If, like me, you prefer to implement the whole thing via PowerShell, the following short command is sufficient: Resize-VHD -SizeByte
e.g: Resize-VHD "D:\HyperV\Test-SRV\Virtual Hard Disks\Test-SRV-Disk0.vhdx" -SizeBytes 60GB

Resize-VHD | Resize virtual hard disk | Hyper-V
The size of VHD files can be managed with Resize-VHD.

After this command has been issued, the (maximum) size of the VHD file is increased to the specified size. ImportantThe new total size of the virtual disk must be specified (not the size to be expanded). The process only takes a few seconds.

Extending the hard disk in the virtual machine

After you have expanded the size of the VHD(X) file of the virtual machine, you must of course make the new, additional storage space available to the operating system. If you have installed a Windows Server version with desktop display, you can do this via disk management. If you have a core installation or simply prefer to use PowerShell, you must first find out which disk and partition you are dealing with. To do this, call with Get-Partition | ft DriveLetter, DiskNumber, PartitionNumber to display a list of the partitions configured in the system.

You can then use Resize partition to adjust the size of the partition. To extend the partition to the maximum size, we read with Get-PartitionSupportedSize the largest possible capacity. The complete command is ultimately:

Resize-Partition -DiskNumber x -PartitionNumber y (Get-PartitionSupportedSize -DiskNumber x -PartitionNumber y).SizeMax.

In my example, I would like to extend the partition with the drive letter E to the largest possible capacity.
I achieve this with:

Resize-Partition -DiskNumber 1 -PartitionNumber 2 (Get-PartitionSupportedSize -DiskNumber 1 -PartitionNumber 2).SizeMax
Extend partitions via PowerShell
Extend a partition to the maximum value using PowerShell.

Immediately after executing this command, the partition is extended and the previously defined additional storage space is available to the operating system.

6 thoughts on “Virtuelle Festplatten in Hyper-V vergrößern mit PowerShell”

  1. Hi,

    das lässt sich hierfür genauso nutzen. Du musst aber zunächst das Volume in der VM auf die neue Größe verringern. Das dürfte der wesentlich schwierigere, bzw. aufwändigere Teil sein. Anschließend kannst Du die fixe Disk mit diesem Befehl verkleinern.

    Resize-VHD -ToMinimumSize

    oder

    Resize-VHD -SizeByte

    Reply
  2. Hallo Tom

    in diesem Fall würde ich darauf tippen, dass die virtuelle Festplatte nicht „dynamisch erweiterbar“ (also mit Thin-Provisioning) angelegt wurde, sondern mit fester Größe (Thick Provisioning).

    In diesem Fall erweitert er die Festplatte zunächst um den gewünschten Speicherplatz, beschreibt aber dann den neuen Speicherplatz einmal komplett mit Nullen, bevor er diesen freigibt. Das dauert dann leider seine Zeit.

    Viele Grüße René

    Reply
  3. Hallo René
    Schön das du nun auch da bist!
    Guter Beitrag!

    Ich habe eine Frage:
    Exchange 2010/Hyper-V-Cluster/Server2008/3Par

    Erweiterung:
    Vorher Offline(wegen 2008)
    Mit Hyper-V-Manger erweitern
    Dabei schlafen einem die Füße ein, das dauert ewig! Keine Ahnung warum.
    ( 100gb ca 10 min.. )
    Geht das per Powershell schneller?

    LG Tom

    Reply

Leave a Comment