How do I mount a VHD to a folder?

What is a VHD file?

VHD (Virtual Hard Disk) is a file format that emulates a physical hard drive. According to the Wikipedia article on VHD file format, “VHD (Virtual Hard Disk) and its successor VHDX are file formats representing a virtual hard disk drive (HDD). They may contain what is found on a physical HDD, such as disk partitions and a file system, which in turn can contain files and folders.”

Essentially, a VHD file allows you to encapsulate an entire operating system and hard drive contents into a single file. This allows you to run an OS inside a file as if it were a physical disk. As Microsoft documentation explains, “The Virtual Hard Disk (VHD) format is a publicly-available image format specification that allows encapsulation of the hard disk contents into a single file for easy transportation.”

The key benefit of VHD files is they provide portability and the ability to emulate physical hard drives virtually. VHDs allow you to quickly deploy disk images and run virtual machines by mounting the VHD files.

Reasons to mount a VHD file

There are several reasons you may want to mount a VHD file in Windows:

Access files stored in the VHD

VHD files are often used as hard drive images for virtual machines. By mounting the VHD file, you can access the files stored within like any other drive on your system without having to run the virtual machine. This allows you to copy files in and out of the VHD easily.

Test a virtual machine

Mounting a VHD allows you to boot up and test a virtual machine without having to launch the hypervisor. This can be useful for troubleshooting boot issues or testing configurations.

Recover data

If a physical system fails, the disk contents can be imaged to a VHD file. Mounting this VHD provides access to recover data off the disk image. This serves as an alternative to booting the image directly.

Overall, mounting VHD files provides direct access to their contents without needing to launch a virtual machine. This is useful for data access, testing, and recovery scenarios.

Requirements for mounting VHD

There are a few key requirements for mounting a VHD file in Windows:

You need administrative privileges on the computer in order to mount a VHD file. Without admin access, you won’t be able to complete the steps to mount the VHD.

You’ll need access to the VHD file itself that you want to mount. VHD files are virtual hard drive files that can contain entire operating systems and data. The VHD needs to be accessible to the computer in order to mount it.

The main utility used for mounting VHD files is the Disk Management console. This built-in Windows utility allows you to attach and mount VHD files as if they were physical disks. So Disk Management needs to be available and accessible on the system in order to mount VHD files.[1]

As long as you have admin access, the VHD file, and Disk Management, you’ll be able to successfully mount the VHD in Windows.

Mounting VHD using Disk Management

Disk Management is a built-in Windows tool that can be used to mount VHD files.

To mount a VHD using Disk Management:

  1. Open Disk Management. In Windows 10 and 11, you can access this by typing “Disk Management” in the search bar and selecting the result.
  2. In Disk Management, locate the VHD file you want to mount under the ‘Disk Drives’ section. All attached physical and virtual disks will be listed here.
  3. Right click on the VHD file and select ‘Attach VHD’ from the context menu.
  4. The VHD will now appear as a new disk drive under the ‘Disk Drives’ section. It will likely show up as ‘Unknown’ and ‘Not Initialized’.
  5. To make the VHD usable, you need to initialize it and create a new volume. Right click on the disk, select Initialize Disk, and follow the prompts.

Once initialized, the VHD will be mounted and accessible as if it were a physical disk drive. You can access files stored on the VHD by navigating to the new drive letter assigned.

For more details, refer to this guide: Manage Virtual Hard Disks (VHD)

Mounting VHD using DiskPart

One way to mount a VHD file in Windows is by using the DiskPart command line tool. Here are the steps to mount a VHD using DiskPart:

1. Open an elevated Command Prompt window. To do this, right-click the Start button and select “Command Prompt (Admin)”.

2. At the command prompt, type diskpart and press Enter. This will start the DiskPart tool.

3. In DiskPart, type attach vhd FILENAME.vhd where FILENAME.vhd is the path and name of the VHD file you want to mount. Press Enter.

4. The VHD file will now be mounted and appear as a new volume in Disk Management and File Explorer. You can access files and folders on the mounted VHD like any other drive.

The attach vhd command mounts the VHD file temporarily. To unmount it, simply close the DiskPart window. Microsoft provides more details on using the attach vhd command in DiskPart.

Mounting VHD using PowerShell

Mounting a VHD file using PowerShell is easy and straightforward. Here are the steps:

1. Open PowerShell as Administrator. This is required to run the Mount-VHD cmdlet.

2. Type the following command:

Mount-VHD -Path PATH_TO_VHD -PassThru

Where PATH_TO_VHD is the full path to the VHD file you want to mount.

3. The VHD will be mounted to the next available drive letter and you can access it like a regular drive.

For example:

Mount-VHD -Path C:\VHDs\disk1.vhd -PassThru

This will mount the VHD file disk1.vhd to the next available drive letter.

The -PassThru parameter passes the newly mounted disk object to the output, allowing you to see details about the mounted VHD disk.

PowerShell makes it very easy to quickly mount a VHD for access without having to use Disk Management or DiskPart.

For more details and examples, refer to the Microsoft documentation (1).

(1) https://learn.microsoft.com/en-us/powershell/module/hyper-v/mount-vhd?view=windowsserver2022-ps

Accessing files in the mounted VHD

Once the VHD file has been successfully mounted, you can access the files and folders contained within it using File Explorer in Windows.

To access the contents of the mounted VHD:

  1. Open File Explorer in Windows.
  2. Look for a new drive letter in the left pane that represents the mounted VHD (for example, if you mounted the VHD to folder Y, you will see a new Y drive).
  3. Double click on the new drive letter to open it.
  4. You will now see all the contents of the VHD file and can access the files and folders contained within.

The VHD basically acts as an additional hard drive that is now attached to your system. You can view, open, edit, copy, move or delete files within the mounted VHD just like you would with a regular hard drive.

Remember to safely unmount the VHD when you are finished accessing the files to disconnect it from your system.

Unmounting the VHD

It is important to properly detach or unmount a VHD file when you are finished using it. Leaving a VHD file mounted can lead to data corruption or other issues. Here are a few methods for detaching a VHD:

Using Disk Management:

  • Open Disk Management (diskmgmt.msc)
  • Right-click on the disk number for the mounted VHD
  • Select “Detach VHD” from the menu

The VHD will now be detached and is no longer accessible as a mounted drive.

You can also detach VHDs from the command line using the Dismount-VHD PowerShell cmdlet. Properly detaching the VHD ensures any writes are flushed and prevents potential data loss or corruption issues.

Alternative virtual disk formats

There are a few other common virtual disk formats besides VHD:

  • VMDK (Virtual Machine Disk) – This format is used by VMware products like VMware Workstation and ESXi. VMDK files store the virtual hard disk data needed to boot an operating system inside a virtual machine. They can be made up of multiple files or housed in one single file. VMDK offers features like sparse allocation of disk space and compression.

  • VDI (VirtualBox Disk Image) – The VDI format is the native disk image format used by VirtualBox. Like VMDK files, VDIs contain a virtual hard disk for booting guest operating systems. VirtualBox VDI files implement a dynamicly allocated model to prevent wasted disk space.

  • QCOW2 (QEMU Copy-On-Write version 2) – The QCOW2 file format is used by QEMU and KVM hypervisors. It provides an efficient way to store disk images by only writing modified blocks to disk. This format also supports disk compression and encryption.

Each hypervisor tends to have its own native virtual disk format. But many support using other formats like VHD as well. The main formats listed above have similar capabilities despite their differences.

When to use VHD vs other solutions

VHD is the native virtual disk format for Microsoft’s Hyper-V hypervisor and Azure cloud platform. As a result, VHD offers tight integration and optimized performance when working with these Microsoft virtualization solutions (cite url: https://www.techtarget.com/searchvirtualdesktop/definition/virtual-hard-disk-VHD).

Some common use cases where VHD shines over other disk formats:

  • Testing new operating systems or applications – VHDs allow you to quickly spin up throwaway test VMs.
  • Migrating VMs between on-prem Hyper-V and Azure – VHD ensures full compatibility.
  • Recovering data from offline VMs – Mounting the VHD gives access to the VM’s files.

However, VHD has some downsides that limit its usefulness for general data storage (cite url: https://www.nakivo.com/blog/work-hyper-v-vhd-vhdx-files-essential-basics/):

  • Max file size limit of 2TB.
  • No built-in redundancy or fault tolerance.
  • Mediocre performance compared to physical disks.

For these reasons, VHD works best as a temporary format for virtual machine disks rather than permanent data storage. Other disk image formats like VMDK or backup formats like VHDX tend to work better for large repositories of long-term data.