DEV Community

vast cow
vast cow

Posted on

How to Create a VHDX on Windows and How to Create a Differencing VHDX

Prerequisites

There are mainly three ways to create a VHDX on Windows.

Method Use case
Disk Management GUI Easy manual creation
PowerShell / Hyper-V module Suitable for automation and creating differencing VHDXs
DiskPart Create using the standard Windows CLI; Hyper-V module not required

When creating a differencing VHDX, the easiest method is usually to use PowerShell’s New-VHD -Differencing. Microsoft’s official documentation also includes an example for creating a differencing VHDX: New-VHD -ParentPath c:\Base.vhdx -Path c:\Diff.vhdx -Differencing. (Microsoft Learn)


1. How to Create a Regular VHDX

Method A: Create with the “Disk Management” GUI

  1. Win + X
  2. Open Disk Management
  3. From the menu, select ActionCreate VHD
  4. Specify the following:
  • Location: C:\VHD\Base.vhdx
  • Size: for example, 64 GB
  • Format: VHDX
  • Type:

    • Fixed size: Allocates the full capacity from the beginning. Prioritizes performance and stability.
    • Dynamically expanding: The file grows according to usage. Suitable for saving space.
      1. After creation, it is added to the disk list
      2. Right-click and select Initialize Disk
      3. Select GPT or MBR
  • Usually, choose GPT

    1. Right-click the unallocated space
    2. Select New Simple Volume
    3. Format it as NTFS or another file system

Method B: Create a VHDX with PowerShell

Run this in PowerShell with administrator privileges.

Create a dynamically expanding VHDX

New-VHD -Path "C:\VHD\Base.vhdx" -SizeBytes 64GB -Dynamic
Enter fullscreen mode Exit fullscreen mode

Create a fixed-size VHDX

New-VHD -Path "C:\VHD\Base.vhdx" -SizeBytes 64GB -Fixed
Enter fullscreen mode Exit fullscreen mode

New-VHD is a Hyper-V PowerShell cmdlet that creates a virtual hard disk. Microsoft’s Hyper-V module also includes New-VHD. (Microsoft Learn)

The following is an example of mounting the created VHDX, initializing it, and formatting it.

Mount-VHD -Path "C:\VHD\Base.vhdx"

$disk = Get-Disk | Where-Object PartitionStyle -eq "RAW" | Sort-Object Number -Descending | Select-Object -First 1

Initialize-Disk -Number $disk.Number -PartitionStyle GPT

New-Partition -DiskNumber $disk.Number -UseMaximumSize -DriveLetter V |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "BaseVHDX" -Confirm:$false
Enter fullscreen mode Exit fullscreen mode

Method C: Create a VHDX with DiskPart

You can create one using the standard Windows diskpart tool without using the Hyper-V PowerShell module. Microsoft’s DiskPart reference explains that create vdisk creates a virtual hard disk and that immediately after creation it is in the same state as an uninitialized disk. (Microsoft Learn)

Run this in Command Prompt with administrator privileges.

diskpart
Enter fullscreen mode Exit fullscreen mode

Run the following inside DiskPart.

create vdisk file="C:\VHD\Base.vhdx" maximum=65536 type=expandable
select vdisk file="C:\VHD\Base.vhdx"
attach vdisk
create partition primary
format fs=ntfs quick label="BaseVHDX"
assign letter=V
exit
Enter fullscreen mode Exit fullscreen mode

Key point:

maximum=65536
Enter fullscreen mode Exit fullscreen mode

is specified in MB, so 65536 MB = 64 GB.

type=expandable
Enter fullscreen mode Exit fullscreen mode

means dynamically expanding. To create a fixed-size disk, use the following.

create vdisk file="C:\VHD\Base.vhdx" maximum=65536 type=fixed
Enter fullscreen mode Exit fullscreen mode

attach vdisk is the command that mounts a VHD/VHDX and displays it as a local disk. (Microsoft Learn)


2. How to Create a Differencing VHDX Based on Another VHDX

Concept of a differencing VHDX

A differencing VHDX uses a parent VHDX as the read source and stores only the changes in the child VHDX.

Base.vhdx        ← Parent disk. Base image
  └─ Diff01.vhdx ← Child disk. Stores only changes
Enter fullscreen mode Exit fullscreen mode

For example, it is suitable for the following uses.

  • Quickly clone verification environments
  • Keep a clean OS image as the parent
  • Create multiple test environments with a small amount of storage
  • If something breaks, you can return to the original state by deleting only the differencing VHDX

Important notes

Before creating a differencing VHDX, you must observe the following regarding the parent VHDX.

Note Reason
Do not modify the parent VHDX Consistency with the child VHDX may break
Do not carelessly move the parent VHDX location The child VHDX may no longer be able to find the parent
Do not delete the parent VHDX The child VHDX becomes unusable
Setting the parent VHDX to read-only is safer Prevents accidental updates
Do not make the differencing chain too deep Causes performance degradation and management complexity

Example of protecting the parent VHDX:

Set-ItemProperty -Path "C:\VHD\Base.vhdx" -Name IsReadOnly -Value $true
Enter fullscreen mode Exit fullscreen mode

To解除する:

Set-ItemProperty -Path "C:\VHD\Base.vhdx" -Name IsReadOnly -Value $false
Enter fullscreen mode Exit fullscreen mode

3. Create a Differencing VHDX with PowerShell

Basic command

New-VHD `
  -ParentPath "C:\VHD\Base.vhdx" `
  -Path "C:\VHD\Diff01.vhdx" `
  -Differencing
Enter fullscreen mode Exit fullscreen mode

Microsoft’s official documentation also shows an example where the parent VHDX is specified with -ParentPath, the differencing VHDX to create is specified with -Path, and -Differencing is specified. (Microsoft Learn)


Mount after creation

Mount-VHD -Path "C:\VHD\Diff01.vhdx"
Enter fullscreen mode Exit fullscreen mode

Once mounted, it appears to Windows like a normal disk.

However, because a differencing VHDX is based on the contents of the parent VHDX, the parent side usually already has partitions and a file system. In that case, initialization and formatting are not required.


Dismount a differencing VHDX

Dismount-VHD -Path "C:\VHD\Diff01.vhdx"
Enter fullscreen mode Exit fullscreen mode

4. Practical Example Using a Differencing VHDX

Example: Create a test environment from a base OS image

# Parent VHDX
$parent = "D:\VHD\BaseWindows.vhdx"

# Differencing VHDX
$child = "D:\VHD\Test01.vhdx"

# Create differencing disk
New-VHD -ParentPath $parent -Path $child -Differencing

# Mount
Mount-VHD -Path $child
Enter fullscreen mode Exit fullscreen mode

To create multiple disks:

New-VHD -ParentPath "D:\VHD\BaseWindows.vhdx" -Path "D:\VHD\Test01.vhdx" -Differencing
New-VHD -ParentPath "D:\VHD\BaseWindows.vhdx" -Path "D:\VHD\Test02.vhdx" -Differencing
New-VHD -ParentPath "D:\VHD\BaseWindows.vhdx" -Path "D:\VHD\Test03.vhdx" -Differencing
Enter fullscreen mode Exit fullscreen mode

The configuration looks like this.

BaseWindows.vhdx
 ├─ Test01.vhdx
 ├─ Test02.vhdx
 └─ Test03.vhdx
Enter fullscreen mode Exit fullscreen mode

Each differencing VHDX has its own independent changes.


5. Check and Modify the Parent Path of a Differencing VHDX

If you move the parent VHDX location, the child VHDX may no longer be able to find the parent.

In that case, you can set the parent path with Set-VHD -ParentPath. Microsoft’s Set-VHD documentation describes the -ParentPath parameter, which specifies the path of the parent disk for a differencing VHD. (Microsoft Learn)

Set-VHD `
  -Path "C:\VHD\Diff01.vhdx" `
  -ParentPath "D:\VHD\Base.vhdx"
Enter fullscreen mode Exit fullscreen mode

6. Merge a Differencing VHDX into the Parent VHDX

If you want to merge the contents of a differencing VHDX into the parent or another differencing disk, use Merge-VHD. Microsoft explains that Merge-VHD is a cmdlet that merges virtual hard disks in a differencing VHD chain. (Microsoft Learn)

Example:

Merge-VHD -Path "C:\VHD\Diff01.vhdx" -DestinationPath "C:\VHD\Base.vhdx"
Enter fullscreen mode Exit fullscreen mode

However, merging directly into the parent VHDX changes the base image. If you want to maintain a clean base, it is safer to take a backup before merging or convert it to another file.


7. Minimal Command Summary

Create a regular VHDX

New-VHD -Path "C:\VHD\Base.vhdx" -SizeBytes 64GB -Dynamic
Enter fullscreen mode Exit fullscreen mode

Mount

Mount-VHD -Path "C:\VHD\Base.vhdx"
Enter fullscreen mode Exit fullscreen mode

Create a differencing VHDX

New-VHD -ParentPath "C:\VHD\Base.vhdx" -Path "C:\VHD\Diff01.vhdx" -Differencing
Enter fullscreen mode Exit fullscreen mode

Mount a differencing VHDX

Mount-VHD -Path "C:\VHD\Diff01.vhdx"
Enter fullscreen mode Exit fullscreen mode

Dismount

Dismount-VHD -Path "C:\VHD\Diff01.vhdx"
Enter fullscreen mode Exit fullscreen mode

Recommended operation

For verification purposes, the following configuration is easy to handle.

D:\VHD\
 ├─ Base\
 │   └─ WindowsBase.vhdx       ← Read-only
 └─ Diff\
     ├─ Lab01.vhdx
     ├─ Lab02.vhdx
     └─ Lab03.vhdx
Enter fullscreen mode Exit fullscreen mode

After completing the parent VHDX, set it to read-only, and perform all experiments on the differencing VHDX side for safer operation.

Top comments (0)