Create Virtual Hard Disk (VHD) using Diskpart in Windows 7

Before you start

Objectives: learn how to create and manage virtual disk using Diskpart command line tool.

Prerequisites: you have to know what a virtual hard disk is.

Key terms: disk, command, create, virtual, diskpart, file, vhd, install, partition, vdisk, drive, select


Run CMD

When running CMD in this case, we have to be sure that we run it with administrative privileges. To do that, right-click on CMD, and select ‘Run as administrator’ option. This will give us elevated command prompt, so we will click on Yes when we get User Account Control prompt.

 Run as administrator

Run CMD as Administrator

From the CMD we will run diskpart. To do that, simply enter “diskpart” and hit Enter.

 Diskpart Command

Enter Diskpart Tool

Once in Diskpart we will run the following command: “create vdisk file=c:\install1.vhd maximum=15000“. This command will create a virtual hard disk file on our C: drive, with the file name “install1.vhd”, and maximum disk size of 15000 MB. We could also add the “type=fixed” or “type=expandable” parameter, but the default is “fixed” so we didn’t write it.

 Command

Create Vdisk Command

Once the VHD creation is complete we will have a install1.vhd file on our C: drive, with 15 GB in size.

 C Drive

C Drive

Now we can attach our virtual disk to the system. To do that first we have to select the disk that we want to attach. To do that we will enter the following command: “select vdisk file=c:\install1.vhd“. This command will select the install1.vhd virtual hard disk so that we can work with it.

 Select Command

Select Command

Now that the virtual disk is selected we can run the attach command. The command is: “attach vdisk“.

 Attach Command

Attach Command

Let’s check the details of our selected virtual disk. To do that we will enter the command: “detail vdisk“.

 Detail Vdisk

Detail Vdisk

At this point our disk is not initialized. We can’t create any partitions or volumes on this disk if we don’t initialize it. To initialize the disk we will enter the command: “convert mbr“. This will convert our disk to basic disk format with the master boot record partition style.

 Convert MBR

Convert Command

Now we can create a partition on the disk. To do that we will use the command: “create partition primary“. We won’t specify the size, so the whole unallocated space will be used to create the partition.

 Create Partition

Create Partition

Now we can format our partition. To do that we will use the command: “format fs=ntfs label=”install” quick“. This command will format our partition using NTFS file system, label it as “install”, and it will use quick formatting.

 Format

Format Partition

Now we can assign a drive letter to it: “assign letter=e

 Drive Letter

Drive Letter

That’s it. We can now use our virtual disk and save files to it. Let’s try to make a new directory in it. To do that we will leave diskpart, and enter few commands.

 E Drive

Working With E Drive

We can now browse to it using Windows Explorer.

 Computer

Computer

E Drive

E Drive

We can also detach virtual disk from our system. To do that we have to go back to diskpart and determine which virtual disk we want to detach. In our case we want to detach install.vhd disk. First we have to select that file: “select vdisk file=”c:\install1.vhd

 Select

Select Command

At this point we can detach the disk using the command: “detach vdisk

 Detach Vdisk

Detach Vdisk

All this can be done using Disk Management tool in Windows 7. We have a separate article in which we show how to create virtual disk using Disk Management.

Remember

To create VHD we can use diskpart. First we have to create VHD file. Then we have to attach it. Before we can create partitions or volumes on it, we have to initialize it. When the disk is initialized, we have to create partition (partitions) or volume (volumes), before we can use it in Windows Explorer. We have to format it using some file system and assign a drive letter to it.