diskpart - Manage Disks and Partitions
Table of Contents
The diskpart command is used to manage disks, partitions, and volumes. You can run many of the operations available in Disk Management from the command line, such as creating and deleting partitions, formatting, and assigning drive letters.
It is also often used to fix problems such as “my USB drive became read-only and I cannot write to it” or “a partition remains that I cannot delete in Disk Management.”
diskpart is a powerful command that can erase the contents of the selected disk instantly. Always confirm the number of the disk you are working on before running anything. If a disk contains important data, we recommend backing it up first.
Basic Usage of diskpart
Use diskpart in a Command Prompt that was run as administrator. If you start it with normal privileges, you will be asked for elevation or get an error. See the related articles for how to start it.
diskpart
After it starts, the prompt changes to DISKPART>. From here on, you enter diskpart-specific commands rather than regular commands.
To quit, type exit.
DISKPART> exit
The Basics: “Select First, Then Run”
Operations in diskpart follow this flow.
- Display a list with a
listcommand - Choose the target with
select - Run an operation such as formatting or deleting on the selected target
Because commands run against whatever you selected with select, it is important to be aware of what is currently selected. The selected item is marked with * at the left edge of the list.
Check the List of Disks
DISKPART> list disk
The columns mean the following.
| Column | Meaning |
|---|---|
| Disk ### | The disk number. This is the number you specify with select disk |
| Status | Online, Offline, and so on |
| Size | The capacity of the disk |
| Free | Capacity not allocated to a partition |
| Dyn | Marked with * for a dynamic disk |
| Gpt | Marked with * for the GPT format (if not marked, it is MBR) |
Disk 0 is often the internal disk that contains Windows, but not always. Use list volume to check which disk C: is on, and identify USB drives and external drives by size (capacity). If you are unsure, unplug and reconnect the USB drive and compare list disk, and the number that appears or disappears is the target disk.
Check Disk Details
Once you select a disk, you can display its detailed information.
DISKPART> select disk 1
DISKPART> detail disk
detail disk shows the model name, type (USB, SATA, NVMe, and so on), whether the disk is read-only, the volumes on the disk, and more. It is useful as a final check before erasing.
Check Partitions and Volumes
With a disk selected, running list partition shows the partitions on that disk.
DISKPART> select disk 1
DISKPART> list partition
When you want to check drive letters and file systems, list volume is handy.
DISKPART> list volume
To work with a volume, specify it by volume number, as in select volume 1, or by drive letter, as in select volume E.
Initialize and Format a USB Drive
This is the procedure for wiping a USB drive completely, creating a new partition, and formatting it. Everything on the disk is erased.
DISKPART> list disk
DISKPART> select disk 1
DISKPART> detail disk
DISKPART> clean
DISKPART> create partition primary
DISKPART> format fs=ntfs quick label=USB_DRIVE
DISKPART> assign
DISKPART> exit
Each command does the following.
| Command | Purpose |
|---|---|
select disk 1 | Sets disk 1 as the target. Always confirm the number |
clean | Deletes all partition and volume information on the disk |
create partition primary | Creates a primary partition that uses all the free space |
format fs=ntfs quick label=USB_DRIVE | Quick-formats as NTFS and applies a label |
assign | Automatically assigns an available drive letter |
clean immediately deletes the partition layout of the selected disk. If you run it while an internal disk is selected, such as select disk 0, Windows may no longer start. Run detail disk right after select disk, and always confirm that the size, model name, and type match the disk you intend to use. diskpart has no confirmation prompt and no undo.
Choosing a File System
For fs= in format, specify a file system that suits your purpose.
| Option | Characteristics |
|---|---|
fs=ntfs | For Windows. Can handle files larger than 4 GB |
fs=exfat | Works well on both Windows and Mac and can handle large files. Suited to USB drives and SD cards |
fs=fat32 | Highly compatible. Each file must be smaller than 4 GB. diskpart cannot format volumes larger than 32 GB as FAT32 |
Adding quick performs a quick format, which finishes in a short time. Omitting quick performs a full format, which takes a long time on large drives. A quick format is usually enough.
Change the Partition Style (MBR / GPT)
If the disk is empty, for example right after clean, you can convert its partition style.
DISKPART> convert gpt
DISKPART> convert mbr
convert fails with an error if partitions or volumes remain on the disk. Empty the disk first with clean or similar before running it. Note that the MBR format cannot use more than 2 TB of space.
When a USB Drive Is Read-Only
A USB drive that you cannot write to may have the “read-only” attribute set on the disk. You can check and clear it with the following steps.
DISKPART> list disk
DISKPART> select disk 1
DISKPART> attributes disk
DISKPART> attributes disk clear readonly
Running attributes disk displays the current state, such as Current Read-only State : Yes. Clearing the attribute with clear readonly lets you write to the drive without erasing its data.
SD cards and some USB drives have a physical write-protect switch. If the switch is in the locked position, you cannot write even after clearing the attribute with diskpart. Check the switch position first.
Delete a Partition
To delete only a specific partition rather than the whole disk, use delete partition.
DISKPART> select disk 1
DISKPART> list partition
DISKPART> select partition 2
DISKPART> delete partition
Protected partitions such as system partitions cannot be deleted with a plain delete partition. Adding delete partition override deletes them, but EFI system partitions, recovery partitions, and others are also erased without confirmation. Do not use it if you do not understand which area you are deleting.
Change a Drive Letter
You can select a volume and assign or remove its drive letter.
DISKPART> select volume 1
DISKPART> assign letter=F
For a volume that already has a drive letter, remove it first and then assign a new one.
DISKPART> select volume 1
DISKPART> remove letter=E
DISKPART> assign letter=F
Changing a drive letter that the system uses, such as C:, may affect how programs work. Do this only for external drives and disks you have added.
Extend and Shrink a Volume
You can select a volume and change its size. The unit is MB.
DISKPART> select volume 1
DISKPART> extend size=10240
DISKPART> select volume 1
DISKPART> shrink querymax
DISKPART> shrink desired=10240
extend size=10240expands the volume by 10240 MB (10 GB). This requires unallocated free space immediately after the volume on the same disk. If you omitsize, it extends using all the available free spaceshrink querymaxdisplays the maximum size by which the volume can be shrunk. Run it first to checkshrink desired=10240shrinks the volume by 10240 MB (10 GB). If the volume contains files that cannot be moved, it may not shrink to the size you want
extend applies to NTFS volumes (and ReFS on newer versions of Windows), and shrink applies to NTFS volumes. FAT32 and exFAT volumes can be neither extended nor shrunk.
Run Automatically with a Script
You can write diskpart commands in a text file and load it with the /s option to run them all at once.
select disk 1
clean
create partition primary
format fs=ntfs quick label=USB_DRIVE
assign
exit
diskpart /s format-usb.txt
diskpart does not show confirmation messages even in interactive mode, but in a script it runs to the end with no chance to check the contents. If the select disk number has shifted at run time, it will erase a disk you did not intend. If disk numbers can change, for example when you plug and unplug USB drives, avoid running the script automatically and proceed by hand, checking as you go.
Quick Reference of Common Commands
| What you want to do | Command |
|---|---|
| Display the list of disks | list disk |
| Select a disk | select disk <number> |
| Display disk details | detail disk |
| Display the list of partitions | list partition |
| Display the list of volumes | list volume |
| Empty a disk | clean |
| Create a partition | create partition primary |
| Format | format fs=ntfs quick label=<label> |
| Assign a drive letter | assign letter=<letter> |
| Remove a drive letter | remove letter=<letter> |
| Clear read-only | attributes disk clear readonly |
| Run a script | diskpart /s <file name> |
| Quit | exit |
clean all writes zeros to every sector of the entire disk. It makes data recovery harder, but depending on the disk’s capacity it can take a very long time, and it is normally unnecessary for SSDs. For an ordinary redo of formatting, clean is enough.