https://www.techtarget.com/searchwindowsserver/tip/Using-Diskpart-to-create-extend-or-delete-a-disk-partition
For basic disk operations on Windows systems, administrators can use diskpart, a command-line utility that remains useful for many different drive-related jobs more than 20 years after it was released.
Microsoft introduced the diskpart management tool in the Windows 2000 OS in 1999 and has included it all versions of Windows since. Diskpart scans for newly added disks, but it can also create, delete and resize drive partitions, as well as assign or reassign drive letters. Diskpart also works with drive volumes, virtual hard disks (VHDs) and removable media, such as USB flash drives. In addition to its management capabilities, diskpart can troubleshoot disk problems, fix corrupted partitions and recover data. The Disk Management utility is also included with Windows OSes and is a similar GUI-based tool, but it lacks the granular control and automation capabilities in diskpart.
Microsoft continues to develop diskpart, and the latest version features 38 commands. To use diskpart, list the objects, such as disks or partitions, and then select an object to change it.
Using diskpart effectively requires a basic understanding of Windows disk terminology. A basic disk is a storage device, such as a hard drive, solid-state drive or USB flash drive, which you format with a Windows file system.
A basic disk can hold one or several partitions in Windows. A partition is a logical structure confined to a single disk.
A volume is a construct of the OS that consists of one or more partitions that can span one or more disks.
You should be careful when using diskpart because there is no undo function if you select the wrong object. For example, if you format a disk, then you lose any data on the disk.
Also, once a command is entered -- even a destructive one -- diskpart does not ask for confirmation before it runs. For example, if you perform an errant diskpart delete partition operation, you must use another utility, such as a third-party disk management program, to recover the data or use a backup image to restore the files from the drive.
There is no override function. You can use the Ctrl+C hotkey or close the command window to stop the ongoing diskpart operation, but this could lead to data corruption or introduce problems with disk functionality.
Follow these tips to avoid issues when using diskpart:
A disk partition sections off a portion of a physical hard drive. Partitioning a hard drive can be done for several reasons, such as organizing data in a more logical fashion or installing a different OS on each partition.
Diskpart performs the tasks of a disk partition manager. Partitioning your disk can help increase the I/O performance of hard disks newly added to RAID. The documentation for many server applications, such as Exchange Server, recommends using diskpart to create the primary or extended partitions. You can use a primary partition as the system partition; you can only use an extended partition for additional logical drive assignments.
The diskpart create command makes disk partitions, volumes on a single disk or multiple disks, and VHDs.
To run diskpart, open a command prompt, and run it as the administrator.
Take the following steps to create a partition with diskpart:
When it comes to adjusting the space for a partition or volume, the following method is superior to configuring two disks. Dynamic disk extensions only concatenate the newly added space, meaning they just add the disk space to the end of the original partition without restriping the data.
Concatenation isolates performance within each partition and does not offer fault tolerance when the partition is configured in RAID. Diskpart restripes your existing data. This is beneficial when the partition is set up in RAID because the existing partition data spreads across all the drives in the array rather than just adding new space to the end, like the Disk Management utility.
Take the following steps to extend a partition with diskpart:
The delete command in diskpart removes dynamic disks, partitions, volumes and shadow copies. When you have multiple volumes on the disk or an unwanted partition, such as a recovery partition, this command removes them and returns them to unallocated space.
Take the following steps to delete a partition:
A volume is a logical construction the OS builds from one or several partitions. In Windows, you can create several volume types with diskpart: simple volume, spanned volume, striped volume, mirrored volume and RAID 5 volume.
A simple volume spans just one partition on a disk.
A spanned volume gathers space from several disks into one logical volume. This operation requires erasing the disks, building the partitions, extending one volume from one disk to the other disk to make the span and then formatting the new volume.
A striped volume, also called a RAID 0 volume, requires two or more disks. This arrangement is often used to boost performance with read/write speeds by spreading data across the drives.
A mirrored volume creates a copy of a volume on another disk, which is helpful to avoid data loss if a disk fails. A mirrored volume requires at least two disks that have the same size.
A RAID 5 volume is similar to a mirrored volume but uses parity information to distribute the data across disks. A RAID 5 volume requires at least three disks. This method is more scalable but has a slower recovery time compared to a mirrored volume.
Take the following steps to create a simple volume of 500 GB with diskpart:
Take the following steps to create a spanned volume of 1 TB using two available disks:
Take the following steps to create a RAID 0 or striped volume using two disks:
Take the following steps to create a mirrored volume using two disks:
Take the following steps to create a RAID 5 volume:
There are several reasons to remove an existing volume on a disk, such as restructuring disk space or switching file systems. Deleting a volume with diskpart permanently deletes any data on the volume and makes the space available for use.
Take the following steps to delete a volume:
When you have an entire disk to wipe clean, you can use the diskpart clean all command to convert all the stored data into unallocated space. This operation deletes all data on the disk by writing zeros on each disk sector. Diskpart also removes all partition and volume information from the selected drive. If security is not a concern, you can use the diskpart clean command, which removes all partitions or volume formatting from the disk.
Take the following steps to clean a disk:
Most operations that change disk structure using diskpart require administrator privileges, so be sure to run diskpart with the right permissions and start from an elevated command prompt to avoid unexpected issues.
It's helpful to check the status of the disk using the list parameter in diskpart before performing any action on a disk, volume or partition.
The attributes disk command shows the properties of the disk and can check if a disk is set to read-only, which prevents any changes. To allow writes to the disk, run diskpart, select the disk and run the attributes disk clear readonly command.
If you encounter a virtual disk service error when trying to perform a diskpart command, then this could mean the disk is being used. To correct this, try closing any running applications, or reboot the device.
An I/O error when running diskpart could mean several things, including a possible hardware failure, but one of the first troubleshooting steps is to run the chkdsk /f /r command, which checks a disk for any errors and recovers information from bad sectors.
For maintenance work that requires a bootable USB flash drive, you can use diskpart to format the partition and set up the file system on the removable drive. Administrators can also use diskpart in both Windows Preinstallation Environment and Windows Recovery Environment to correct disk problems or to configure the machine for deployment.
With the diskpart /s switch, administrators can run scripts to automate tasks associated with setting up Windows computers, such as configuring the disks for multiple systems, adding a recovery partition or wiping all data from a disk to return it to a factory state.
Sample code from the Microsoft site gives examples of how to develop a text-based script file administrators can call from diskpart and run automatically. IT workers can run several diskpart scripts, but Microsoft recommends building in a delay of 15 seconds after a script runs to prevent issues.
Editor's note: This article was updated in August 2024 to improve the reader experience.
14 Aug 2024