Diskpart is a command-line tool used that can be utilised for disk management. Supported commands include creating, formatting, resizing and deleting partitions, managing Virtual Hard Disks (.vhd files), changing disk type (GPT or MBR), etc.
Diskpart is used by selecting a device (this could be a disk, partition or volume) - any subsequent commands will be executed on the selected device. Care needs to be taken to ensure that the correct device is selected as Diskpart commands can result in data loss. The clean command for example will erase a number of sectors from the selected disk - effectively removing the partition table and Master Boot Record. The clean all command will write zeros to the entire disk - securely erasing ALL data.
After selecting a device, commands can be used to directly manipulate it. If disk 0 is selected then further commands will be carried out on this disk - e.g. clean will wipe all partition information from disk 0, create partition primary will create a partition on disk 0 (if there is any free space), etc. If an individual partition is selected then further commands will be carried out on this partition - e.g. delete partition (to delete it), assign (to assign a drive letter if the partition is not mounted), format (to format it), etc.
It's possible to select different device types at the same time. A disk (disk 0) and a partition (partition 3) are both selected in the DiskPart output below for -
Running a command can automatically select a device. Running the command create partition primary for example, will create a parition and will also select the new partition. Running the command create partition primary immeadiately followed by active will set the new partition as active without manually selecting it first.
The following examples illustrate some of the basics, including creating a Virtual Disk, and cover the commands that will be used in this guide. The list and detail commands are particularly useful to identify the correct target.
Run the list disk command to check available disks. In the sample output below the system has only one disk (Note - Disks are numbered from 0) -
Select the target device, in this case disk 0, using the command select disk 0 -
In the preceding step, disk 0 was selected. For more detailed information about the disk now that it has been selected, use the detail disk command -
Sample output after running the detail disk command without having selected a disk -
Use the list partition command to check partition layout (ensure that a disk is selected before running the command - e.g. select disk 0 > list partition). Note - Partitions are numbered from 1 -
Create a virtual disk (.vhd) D:\windows.vhd > fixed type disk > size 20480 MiB (automatically selecting the new disk in the process) -
Mount the Virtual Disk created in the preceding step (D:\windows.vhd) -
Having attached a Virtual Disk created in the preceding steps, lets (re)check for available disks. Run the list disk command (note the * next to Disk 1 - indicating that this is the selected disk type device) -
If disk 1 hadn't already been selected, we could select the Virtual Disk using the command select disk 1
Run the command detail disk for more detailed information about the disk selected in the preceding step -
Create a primary partition on the selected disk (disk 1) using the command create partition primary (automatically selecting the new partition in the process) -
Mark the partition created in the preceding step as active -
Format the selected partition (NTFS file system > quick format) -
Mount the selected partition as W:\
List partitions on the selected disk (disk 1) -
Document date - 29th June 2019