Diskpart command

Updated: 11/12/2023 by Computer Hope
diskpart command

DiskPart is a command-line disk-partitioning utility available for Microsoft operating systems. Use it to view, create, delete, and modify a computer's disk partitions.

Availability

The diskpart command is available in the Recovery Console and Command Prompt of the following Microsoft operating systems.

How to use DiskPart

Your Windows user account must be a member of the Administrators group to use DiskPart.

The DiskPart executable file, diskpart.exe, has two options:

diskpart [/s script] [/?]
/s script Execute the DiskPart commands contained in the text file named script, one line at a time.
/? Display the help message.

If no options are specified, running diskpart starts the DiskPart interactive command prompt, where DiskPart commands run. For more information about how to start DiskPart and run commands, see examples.

DiskPart commands

Note

Most of the following DiskPart commands require you select a disk or partition to give it "focus." To view the disks and partitions that can be selected, use the list command.

By default, if DiskPart encounters an error, it terminates with a non-zero exit status.

Some DiskPart commands accept the noerr option. If noerr is specified, DiskPart ignores any errors encountered, and attempt to continue with subsequent operations. This option is a potentially dangerous and should be used with caution.

Command Subcommands, syntax, and description.
active active
Mark the selected partition as "active," indicating to your computer's BIOS or UEFI that this partition contains a bootable system. For example, the partition where Microsoft Windows is installed is an active partition. Only partitions on a basic disk (as opposed to a dynamic disk, such as a RAID array) can be marked active. DiskPart does not actually check the contents of the partition to verify that it contains bootable system files.
add add disk=n [align=n] [wait] [noerr]
Create a mirror of the selected disk object at partition n, which must be a simple volume. Partition n must have unallocated space equal to or greater than the size of the selected volume, or the mirror cannot be created. If successful, the selected simple volume becomes a mirrored volume, with the mirror stored in partition n. This command is not valid in Windows Vista.
assign assign [letter=d | mount=path] [noerr]
Assign a drive letter or mounted folder pathname to the selected volume. If no drive letter d or mounted folder path is specified, the next available letter is assigned. If the drive letter or mounted folder is already in use, the operation fails and reports an error. Drive letters assigned to removable drives can be changed. Drive letters cannot be assigned to boot volumes or volumes containing the Windows paging file. Drive letters cannot be assigned to an OEM partition (exception: Windows PE (pre-installation environment), any GPT (GUID partition table) partition other than a basic data partition, an ESP partition, or a recovery partition.
attributes attributes disk [set | clear] [readonly] [noerr]
Set, clear, or display the attributes (flags) of the selected disk. The readonly flag indicates that the disk is write-protected (read-only). If set is specified, the flag is set. If clear is specified, the flag is unset. If no options are given, the current disk flags are displayed. Disk flags other than "readonly" may be displayed, but cannot be set or cleared.
attributes volume [{set | clear}] [{hidden | readonly | nodefaultdriveletter | shadowcopy}] [noerr]
Set, clear, or display the attributes (flags) of the selected volume. The readonly flag indicates that the volume is write-protected. The hidden flag indicates that the volume should not be displayed in volume lists, e.g., in File Explorer. The nodefaultdriveletter flag indicates that the volume should not automatically be mounted and assigned a drive letter. The shadowcopy flag indicates that the volume is a shadow copy volume, used by the Windows VSS service. If set is specified, these flags are set. If clear is specified, these flags are unset. If no options are given, the current volume flags are displayed. On basic MBR disk, the hidden, readonly, and nodefaultdriveletter attributes always apply to all volumes on the disk. On basic GPT disks, dynamic MBR disks, and dynamic GPT disks, the attributes apply only to the selected volume.
attach attach vdisk [ readonly ] [sd=sddl-string] [usefilesd] [noerr]
Attaches (mounts) a VHD or VHDX (Hyper-V virtual hard disk) file. The attached VHD appears as a local hard drive. If the VHD already has a disk partition with a valid file system, the virtual disk is as assigned a drive letter. If readonly is specified, the disk is mounted in read-only mode. If sd is specified, and sddl-string is a valid SDDL security descriptor string, that descriptor is applied to the entire disk. If usefilesd is specified, the security descriptor of the VHD file itself is applied to the entire disk. For more information about security descriptors, see create vdisk, below.
automount automount [enable | disable | scrub] [noerr]
Configure the AutoMount feature, which automatically mounts and assigns drive letters to disks that are newly attached to the system. If enable is specified, new disks will be automatically mounted, and assigned a drive letter and a volume GUID pathname. If disable is specified, this feature is disabled. If scrub is specified, any existing folder pathnames, drive letters, mounted folder directories, and registry settings are removed for volumes no longer connected to the system. Before Windows Vista, the AutoMount feature is available on basic disk volumes only. In Vista and newer versions, AutoMount is also available on dynamic disk volumes.
break break disk=n [nokeep] [noerr]
Break the selected mirrored volume into two simple volumes. Applicable to dynamic disks only. One of the simple volumes retains the mirrored volume's assigned drive letter, GUID pathnames, and mounted folder paths, if any. The other volume is selected; assign it a drive letter. When you do, a GUID pathname is automatically assigned. By default, both simple volumes retain the data of the original mirrored volume. If the nokeep option is specified, only one simple volume retains the data, and the other simple is cleared of data, and not selected.
clean clean [all]
Remove all partitioning and volume formatting from the selected disk. If all is specified, every byte on the disk is overwritten to zero, destroying any existing information on the entire disk. On MBR disks, only the MBR partitioning information and hidden sector information are overwritten. On GPT disks, in addition to the GPT information, the Protective MBR is overwritten. If all is omitted, only the first and last 1 MB of the disk is overwritten to zero. After a successful clean, the status of the disk is listed in DiskPart as "UNINITIALIZED."
compact compact vdisk
Attempt to reduce the size of the selected expandable VHD file. The VHD must be expandable for the operation to succeed. Even if a virtual disk is mounted as read-only, the VHD file can be compacted.
convert convert basic [noerr]
Convert the selected empty dynamic disk to a basic disk. All data on the selected disk must be deleted from the disk, and all its partitions and volumes deleted, or the operation fails.
convert dynamic [noerr]
Convert the selected basic disk to a dynamic disk. Any volumes on the disk become simple volumes.
convert gpt [noerr]
Convert the selected empty basic MBR disk to a basic GPT disk. All data on the selected disk must be deleted from the disk, and all its partitions and volumes deleted, or the operation fails.
convert mbr [noerr]
Convert the selected empty basic GPT disk to a basic MBR disk. All data on the selected disk must be deleted from the disk, and all its partitions and volumes deleted, or the operation fails.
create create partition efi [size=n] [offset=n] [noerr]
Create an EFI (Extensible Firmware Interface) system partition on the focused basic GPT disk. The partition begins at the disk region offset from the beginning of the disk by n kilobytes if offset is specified, otherwise at the first available location on disk of adequate size. The size of the partition is n megabytes if size is specified, otherwise the partition uses all available space on that region of the disk. If the operation is successful, the new partition is focused.
create partition extended [size=n] [offset=n] [align=n] [noerr]
Create an extended partition on the focused basic MBR disk. Only one extended partition may exist on a disk (divide it into logical partitions later). The partition begins at n kilobytes if offset is specified, otherwise at the first available location of adequate size. The size is n megabytes if size is specified, otherwise it occupies all available space on disk beginning at its offset. If align is specified, the partition's offset is rounded to a multiple of n kilobytes, which may improve performance when using a RAID configuration with LUNs (logical unit numbers).
create partition logical [size=n] [offset=n] [align=n] [noerr]
Create a logical partition in the extended partition of the focused basic MBR disk. The partition begins n kilobytes from the beginning of the extended partition if offset is specified, otherwise at the first available space in the extended partition. The size is n megabytes if size is specified, otherwise it uses all available space in the extended partition beginning at the offset.
create partition msr [size=n] [offset=n] [noerr]
Create an MSR (Microsoft reserved) partition on the focused GPT disk. The partition begins at n kilobytes if offset is specified, otherwise at first available location on disk. The size is n megabytes if size is specified, otherwise all available space is used. Use this command with extreme caution, because GPT disks have strict requirements for partition layout, and a layout which violates these rules may cause the disk to become unbootable. On GPT disks used to boot Windows, the EFI system partition must be the first partition on disk, followed immediately by the MSR partition. On GPT disks that do not have an EFI system partition (used for data storage only, no bootable OS on disk), the MSR partition must be the first partition on disk.
create partition primary [size=n] [offset=n] [id={byte|guid}] [align=n] [noerr]
Create a primary partition on the focused basic disk. The partition begins at n kilobytes if offset is specified, otherwise at the first available space on disk. The size is n megabytes if size is specified, otherwise all available space beginning at the offset is used. If align is specified, the offset will be rounded to a multiple of n kilobytes, which can improve performance on RAID configurations that use LUNs. The default partition type is "no filesystem" for MBR disks, or "basic data partition" for GPT disks.

When the partition is successfully created, it is automatically focused by DiskPart. A drive letter must manually be assigned with the assign command.

The partition type may be manually specified with the id option. Note that this option is intended for OEM use only. For MBR disks, the partition type byte is specified as a hexadecimal value with the leading 0x removed (e.g., 06 for "no filesystem," or 27 for "recovery partition"). For GPT disks, partition type is specified by a guid value (such as ebd0a0a2-b9e5-4433-87c0-68b6b72699c7 for "basic data partition," or de94bba4-06d1-4d40-a16a-bfd50179d6ac for "recovery partition"). To see a list of recognized partition types, run help create partition primary.

Use caution when using the id option. The byte or guid value you specify will be set, even if it's unrecognized or incorrect. Incorrect use of the id option can cause the computer to become unbootable.
create volume raid [size=n] disk=n,n,n[,n[,...]] [align=n [noerr]
Create a raid-5 volume from three or more dynamic disks. The disks to be used must be specified with the disk parameter, with drive numbers separated by commas, e.g., disk=3,4,7 or disk=5,6,7,8. If align is specified, all volume extents are rounded to a multiple of n kilobytes, which may improve performance with a hardware RAID LUN configuration. When the operation succeeds, the new volume is automatically focused. Note that RAID-5 volumes are not supported in Windows Vista.
create volume simple [size=n] [disk=n] [align=n] [noerr]
Create a simple volume on the focused dynamic disk, or the disk specified with the disk parameter. Its size is n megabytes if size is specified, otherwise it uses all available space on the disk. If align is specified, the volume boundaries will be rounded to a multiple of n kilobytes, which can improve hardware RAID performance. When the volume is successfully created, the new volume is automatically focused.
create volume stripe [size=n] [disk=n,n,n[,n[,...]]] [align=n] [noerr]
Create a striped volume using two or more dynamic disks. The disk numbers must be specified by the disk parameter as a comma-separated list, e.g., disk=3,4 or disk=4,5,7. The size of the volume is n megabytes if size is specified, otherwise it uses the maximum free space on the smallest disk, and an equal amount of space on the other disks. If align is specified, volume boundaries are rounded to multiples of n kilobytes. When the volume is successfully created, it is automatically focused.
create volume mirror [size=n] disk=n,n,n[,n[,...]] [align=n] [noerr]
Create a mirror volume using two dynamic disks. The disks must be specified by number with disk, e.g., disk=2,3 or disk=3,5. The size of the partition is n megabytes if size is specified, otherwise it uses the remaining free space on the smaller disk, and an equal amount of space on the other. If align is specified, volume boundaries are rounded to a multiple of n kilobytes, which can improve performance in hardware RAID configurations that use LUNs. When the volume is successfully created, the new volume is automatically focused.
create vdisk file="filename" maximum=n [type={fixed|expendable}] [sd=sddl-string] [parent="parentfile"] [source="sourcefile"] [noerr]
Create a virtual disk file. The VHD and VHDX file formats are supported, and are specified by file extensions .vhd or .vhdx. The destination file name is specified with the required file parameter, where destfile is the complete path and file name of the destination virtual disk file, e.g., "C:\my-vdisk.vhd." The maximum parameter sets the maximum disk space exposed by the virtual disk to n megabytes. The type parameter specifies if the disk size is fixed (size can never be changed, default) or expandable (free space in the disk file can be expanded or compacted later).

If source is specified, the new virtual disk is populated with data from existing virtual disk sourcefile. If parent is specified, the virtual disk is created as a Hyper-V differencing disk, containing only the block data altered from parentfile, an existing virtual disk of the same size. The security descriptor of the virtual disk is set to match the parent directory of the destination file, or to the SDDL format string sddl-string if sd is specified. For details about valid SDDL strings, run help create vdisk.
delete delete disk [override] [noerr]
Delete the focused missing dynamic disk from the disk list. If override is specified, DiskPart deletes all simple volumes on the disk. If the disk contains half of a mirrored volume, the half of the mirror on the disk is deleted. An override fails if the disk is a member of a RAID-5 volume.
delete partition [override] [noerr]
Delete the focused basic disk partition. If it's a system partition, boot partition, or if it contains an active paging file or crash dump file, it cannot be deleted. Partitions on dynamic disks cannot be deleted.
delete volume [noerr]
Delete the focused volume. If it's a system volume, boot volume, or contains an active paging file or crash dump file, it cannot be deleted.
detail detail disk
Display the properties of the focused disk, and list its volumes.
detail partition
Display the properties of the focused partition.
detail volume
Display the properties of the focused volume, and list the disks on which the volume resides.
detail vdisk
Display the properties of the focused virtual disk.
detach detach vdisk [noerr]
Detach the specified virtual disk.
exit exit
Exit DiskPart.
extend extend [size=n] [disk=n] [noerr]
Extend the focused volume or partition, and optionally its file system, into unallocated space on a disk. The volume is expanded by n megabytes if size is specified, otherwise all available space is used. If disk is specified, and the focused volume or partition is a dynamic disk, the extension occurs on disk number n; otherwise, it occurs on the original disk. On basic disks, the expansion must always occur on the same disk, in space immediately following the original.

On dynamic disks with simple or spanned volumes, a volume can be extended to any free space on any dynamic disk. Mirrored, RAID-5, and striped volumes cannot be extended.

If the partition was previously formatted with NTFS, expand fails, and make no changes.
extend filesystem [noerr]
Extend the file system of an expanded volume or partition. Run this after the original expand command to expand the file system as well.
expand expand vdisk maximum=n
Expand the maximum size available on the focused virtual disk. The required maximum parameter sets the new total size to n megabytes. For example, maximum=30000 sets the new maximum size of the virtual disk to 30 gigabytes.
filesystems filesystems
Display information about the file system of the selected volume, and a list of supported file systems that can format it.
format format [fs=fstype] [revision=X.XX] | [recommended] [label="label"] [unit=n] [quick] [compress] [override] [duplicate] [nowait] [noerr]
Format the focused volume. The file system fstype is used if fs is specified, otherwise the default file system listed by the filesystems command is used. If a specific revision of the file system is desired, it can be specified with revision. If label is specified, the volume label is set to label. If unit is specified, the default allocation unit size is overridden and set to n bytes. If quick is specified, a quick format is performed. If compress is specified and fs=NTFS, the NTFS file system will compress files by default. If override is specified, DiskPart attempts to force unmount the volume before formatting. If duplicate is specified and fs=UDF (Universal Disk Format) revision 2.5 or higher, file system metadata is duplicated to a second set of sectors on the disk (metadata redundancy). If nowait is specified, the command returns to the prompt immediately, and performs the format in the background.
gpt gpt attributes=n
Assign the specified GPT attributes to the focused partition on a basic GPT disk. This command is intended for OEM or IT professional use only. For detailed information about valid hexadecimal values of n, run help gpt.
help help [command] [subcommand ...]
Display a list of commands and subcommands with the syntax and descriptions. For example, help, help help, help create, or help create partition efi.
import import [noerr]
Import all disks in the same disk group as the focused foreign disk into the local computer's online disk group. A "foreign disk" is any dynamic disk installed locally that was previously used on another computer, or another version of Windows. After import, the dynamic disk volumes become visible and accessible.
inactive inactive
Mark the focused partition of a basic MBR disk as inactive. Note, if the partition is a system or boot partition, the computer may not be able to boot until the disk is marked active again.
list list disk
Display a list of disks that can be accessed by DiskPart, and information about them. The focused disk is marked with an asterisk (*).
list partition
Display a list of accessible partitions on the focused disk. On dynamic disks, partitions listed may not exactly correspond to the dynamic volumes on the disk. Partitions cannot be created or deleted on a dynamic disk.
list volume
Display a list of basic and dynamic volumes mounted on the local system.
list vdisk
Display a list of virtual disks. The disk type is listed as "unknown" until the disk is attached. Any disks with focus are marked with an asterisk (*).
merge merge vdisk depth=n
Merge the focused child Hyper-V differencing disk with its parent virtual disk. The depth parameter may be used if the disk has multiple parents, to specify how deeply the child should be merged. For instance, depth=2 would merge the child with its parent and grandparent. If not specified, the default depth is 1, which merges the child with its parent.
online online disk [noerr]
Bring the focused offline disk (in offline SAN mode) online. If the disk is dynamic, read-only, and offline, and you want to bring it online in read-write mode, first enable read-write mode, then bring the disk online. An offline read-only dynamic disk will report an error if brought online, because the operation requires writing to the dynamic disk database on the disk.
online volume [noerr]
Bring the focused offline volume online. Not applicable to OEM, ESP, or recovery partitions.
offline offline disk [noerr]
Take the focused online disk (in online SAN mode) offline. Not applicable to OEM, ESP, or recovery partitions.
offline volume [noerr]
Take the focused online volume offline.
recover recover [noerr]
Refreshes the state of all disks in the pack which contains the selected dynamic disk. Here, "disk pack" refers to a set of dynamic disks used in a mirroring or RAID-5 configuration. The recover command attempts to resynchronize any volumes in the pack that have stale plex or parity data.
rem rem [remark]
Does nothing. Use rem to include comments in a DiskPart script.
remove remove [letter=d | mount={path|all}] [dismount] [noerr]
Remove mount points of the focused volume, and optionally dismount (unmount) the volume. If letter is specified, the drive letter d is removed, or if mount is specified, the mounted pathname path is removed. If all is specified, all mount points are removed for the volume. The dismount option attempts to unmount the volume, which is only possible if all mount points are removed. Mount points for system or boot volumes, or volumes containing an active paging file, cannot be removed.
repair repair disk=n [align=n] [noerr]
Attempt to repair the focused RAID-5 volume containing a failed disk, by replacing the failed disk with another, specified disk. The required parameter disk specifies the disk number, n, which is to replace the failed disk. The replacement disk must have free space greater than or equal to the size of the failed disk. If the align parameter is specified, volume extents are aligned to a multiple of n kilobytes, which can increase performance in hardware RAID configurations that use LUNs.
rescan rescan
Rescan the computer for accessible disks and volumes.
retain retain
Prepare the focused dynamic simple volume to be used as a system or boot volume.
san san [policy={OnlineAll | OfflineAll | OfflineShared | OfflineInternal}] [noerr]
Display or set the SAN policy for the currently booted operating system. With no parameters, the current SAN policy is displayed. The policy parameter may be specified to set the SAN policy. The default policy for Windows Advanced Server and Windows Data Center is OfflineShared, where the boot disk is brought online and all disks not on a shared bus in read-only mode. On all other versions of Windows, the default policy is OnlineAll, where all disks are brought online, in read-write mode. The OfflineAll policy specifies that all disks except the boot disk are taken offline, and are read-only by default. The OfflineInternal policy specifies that all newly discovered internal disks are kept offline and read-only by default.
select select disk {n | system | next | path}
Select (focus) a disk. The disk may be specified by disk number, n. If the disk is specified as system, on BIOS computers, BIOS disk 0 is focused, or on EFI machines, the disk containing the ESP partition is focused. If the disk is specified as next, and a disk currently has focus, the next disk in the list will be focused.
select partition n
Select (focus) partition n on the currently focused disk.
select volume={n | d}
Select (focus) a volume. The volume may be identified by volume number n, or by drive letter or mounted folder pathname d. For example, volume=2, volume=C, or volume=C:\MyMount.
select vdisk file="filename" [noerr]
Select (focus) a virtual disk. The disk is specified with the file parameter, where filename is the complete path and file name of the virtual disk file. For example, file="C:\vhd\myVdisk.vhd".
set set id={byte|guid} [override] [noerr]
Change the partition type of the focused partition. Note, this command is intended for use by OEMs or IT professionals only. The type is specified with the id parameter. If it's an MBR partition, the type is a hexadecimal byte value with the leading 0x removed, e.g., 06 for "no filesystem." Note, the byte value 42 (LDM partition type) cannot be set with this command. If the partition is a GPT partition, the type can be set with a guid value, e.g., ebd0a0a2-b9e5-4433-87c0-68b6b72699c7 for "basic data partition." Note, DiskPart does not check byte or guid for validity, and may set the value even if it's invalid or incorrect, which may cause data loss. Do not use this command if you are not an equipment manufacturer or IT professional. The proper way to set partition type is at creation time; see create partition, above, for more information.

Before DiskPart attempts to change the partition type, it attempts to lock and unmount the file system. If the lock and unmount fails, the partition type is not set, and an error is reported. However, if override is specified, DiskPart attempts to force-unmount the file system, even if it will not unmount gracefully.

The command setid is an alias for set id.
shrink shrink [desired=target] [minimum=min] [nowait] [noerr]
Reduce the maximum size of the focused volume, removing some or all the free disk space it contains. If no parameters are specified, the size is reduced to eliminate all free space. If desired is specified, DiskPart tries to shrink the volume so its new total size is target megabytes. If minimum is specified, DiskPart tries to reduce the original size by n megabytes.

Note, some files, for example the paging file or the shadow copy storage area, sometimes cannot be relocated. These "unmovable" files prevent DiskPart from consolidating the free space, which can interfere with the shrinking process. If the shrink fails, view your Application Logs (in Event Viewer, click Windows Logs then Application) and search for Event 259 to identify the unmovable file that aborted the shrink process. Resolve the issue by reconfiguring the paging file. (In File Explorer, right-click This PC or My Computer, and choose Properties to open the System Properties. Click Advanced system settings. In the Advanced tab, Performance section, click Settings. In the Advanced tab, Virtual memory section, click Change). Unmovable shadow copy files may be reconfigured, moved, or deleted (e.g., with wmic shadowcopy, or in System Properties, under System Protection).
shrink querymax [noerr]
Display the maximum number of bytes that can be removed from the focused volume. The amount displayed represents the amount of free space currently available in the volume. If the volume is currently in use, this value may vary as writes occur on the disk.
uniqueid uniqueid disk [id={dword | guid}] [noerr]
Displays or sets the GPT (GUID partition table) identifier or MBR (master boot record) signature of the focused disk. If no parameters are provided, the ID of the focused disk is displayed. If the id parameter is specified, the ID is set to the four-byte hexadecimal value dword for MBR disks, or to GUID guid for GPT disks.

Examples

To start DiskPart, run the diskpart command, from the Run box or a Command Prompt. In Windows versions previous to Windows 10, the command requires you run a Command Prompt as Administrator. In Windows 10, if diskpart is run by a non-administrator, answer Yes at the UAC prompt to proceed.

diskpart

You are presented with the DISKPART> prompt, where DiskPart commands are entered and run.

DiskPart commands

list disk

List available disks. Sample output:

  Disk ###  Status         Size     Free     Dyn  Gpt
  --------  -------------  -------  -------  ---  ---
  Disk 0    Online          476 GB   449 MB
  Disk 1    Online         2047 GB      0 B
  Disk 2    No Media           0 B      0 B
  Disk 3    No Media           0 B      0 B
  Disk 4    No Media           0 B      0 B
  Disk 5    No Media           0 B      0 B
select disk 0

Select (focus) Disk 0. Subsequent commands that operate on a focused disk use this disk.

Disk 0 is now the selected disk.
list partition

List the partitions on the focused disk. Sample output:

  Partition ###  Type              Size     Offset
  -------------  ----------------  -------  -------
  Partition 1    Primary            549 MB  1024 KB
  Partition 2    Primary            195 GB   550 MB
  Partition 0    Extended           269 GB   195 GB
  Partition 4    Logical             29 GB   195 GB
  Partition 5    Logical             29 GB   225 GB
  Partition 6    Logical              8 GB   254 GB
  Partition 7    Logical            202 GB   262 GB
  Partition 3    Recovery           502 MB   465 GB
select partition 1

Select (focus) partition 1 on the focused disk.

Partition 1 is now the selected partition.
detail partition

Display detailed information about the focused partition. Sample output:

Partition 1
Type  : 07
Hidden: No
Active: Yes
Offset in Bytes: 1048576
  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
* Volume 0         System Rese  NTFS   Partition    549 MB  Healthy    System