diskpart

On this page 8

diskpart is the built-in Windows command-line utility for managing disks, partitions and volumes. It runs as an interactive text interpreter in which the administrator first selects an object and then acts on it, which makes it a core tool of PowerShell & command line administration on Windows whenever the graphical Disk Management console is unavailable or too limited.

The tool ships with every supported edition of Windows and with Windows PE and Windows Recovery Environment, so it remains available when the installed system will not boot. It requires administrator privileges, and because most of its commands take effect immediately without confirmation, it is also one of the most destructive tools an administrator can type into a console.

How diskpart works through object focus

diskpart operates on a focus model: a command such as select disk 1 moves focus to an object, and every following command applies to that object until focus changes. Objects form a hierarchy of disks, partitions and volumes, and selecting a partition implicitly sets focus on its parent disk as well.

The list commands show what exists, and an asterisk marks the object in focus. This visual marker is the main safeguard against operating on the wrong drive, so experienced administrators run list disk and detail disk before any change. Disk numbers come from the order of enumeration, not from a stable identifier, and they can change after a reboot or when a USB drive is attached.

The following table summarizes the three object types and how commands address them.

ObjectSelection commandTypical actions
Diskselect disk 0clean, convert gpt, online, attributes
Partitionselect partition 2create, delete, format, set id
Volumeselect volume Cassign letter, extend, shrink, format
Virtual diskselect vdisk file=pathattach, detach, compact, expand
Object types in diskpart

Essential diskpart commands

A small set of commands covers most preparation and repair work. The typical sequence for a new data disk is select, clean, convert, create, format and assign, and each step maps to a single command line.

CommandEffectRisk level
list diskShows all disks with size and free spaceNone
cleanRemoves the partition table from the selected diskVery high
clean allWrites zeros to every sector of the diskVery high
convert gptChanges an empty disk to GUID Partition TableHigh
create partition primaryCreates a partition in free spaceMedium
format fs=ntfs quickCreates a file system on the partitionHigh
assign letter=EGives the volume a drive letterLow
extend / shrinkChanges volume size into adjacent spaceMedium
Frequently used commands and their effect

The difference between clean and clean all matters in practice. Plain `clean` only erases partitioning metadata in the first and last sectors and finishes in seconds, whereas `clean all` overwrites the whole disk, which can take hours on large hard drives and is still not a certified sanitization method for solid-state drives, where firmware-level secure erase is more appropriate.

Other useful commands include detail disk for identifying a disk by model and bus type, attributes disk clear readonly for releasing write-protected media, and set id for marking a partition as an EFI System Partition or a recovery partition during manual deployment.

Scripting diskpart for automation

diskpart supports unattended execution through the /s switch, which reads commands from a text file: diskpart /s layout.txt. Deployment teams use such answer scripts to build the standard UEFI layout of an EFI System Partition, a Microsoft Reserved Partition, a Windows partition and a recovery partition in Windows PE before applying an image.

Script mode has weaknesses that interactive use hides. diskpart stops at the first failing command unless the noerr parameter is added, its exit codes are coarse, and it returns plain text rather than objects, so a calling batch file must parse output with string matching. A script that relies on a fixed disk number can also wipe the wrong drive on hardware with a different enumeration order.

diskpart sits between the graphical console and the modern Storage module cmdlets in PowerShell. The comparison below shows when each tool fits best.

ToolInterfaceStrengthsWeaknesses
diskpartInteractive text or scriptAvailable in Windows PE, low dependenciesText output, no object pipeline
Storage cmdletsPowerShellObjects, filtering, remote useNot present in minimal Windows PE
Disk ManagementGraphical consoleVisual overview, fewer typing errorsNot scriptable, limited operations
DISMCommand lineApplies and services imagesDoes not partition disks
Disk management options on Windows

Cmdlets such as Get-Disk, Initialize-Disk, New-Partition and Format-Volume provide the same operations with object output and the `-WhatIf` safety switch, which diskpart lacks entirely. For new automation on a full Windows installation, PowerShell is the preferred choice, while diskpart remains the dependable fallback in recovery environments and older deployment pipelines. Its predecessor fdisk from MS-DOS has no role on current Windows.

Partition styles, limits and file systems

diskpart works with both MBR and GPT partition styles, and the choice determines boot mode and capacity. A disk must be empty before conversion with convert gpt or convert mbr; in-place conversion of a system disk without data loss requires the separate MBR2GPT tool instead.

PropertyMBRGPT
Maximum primary partitions4, or 3 plus an extended partition128 by default on Windows
Maximum addressable sizeAbout 2 TiB with 512-byte sectorsFar beyond current disk sizes
Boot firmwareLegacy BIOSUEFI
Redundant partition tableNoYes, primary and backup header
Practical limits that diskpart enforces

The format command offers NTFS, FAT32 and exFAT, but diskpart refuses FAT32 on volumes larger than 32 GB, a limit inherited from Windows formatting tools rather than from the file system. For a larger FAT32 volume, such as one needed by certain UEFI firmware or embedded devices, administrators use third-party utilities.

Common mistakes and safe practice

Most incidents with diskpart come from selecting the wrong object, not from defects in the tool. The table lists frequent problems with their causes and the correct response.

SymptomCauseRemedy
Data disk wiped instead of USB driveDisk numbers assumed, not verifiedCheck size and model with detail disk first
Extend fails with an errorFree space is not adjacent to the volumeMove or delete the blocking partition, or use cmdlets
Media are write-protectedRead-only attribute set on the diskRun attributes disk clear readonly
New disk not visible in ExplorerDisk offline or uninitializedRun online disk, then create and assign
Recovery partition blocks extensionPartition sits after the Windows volumeRelocate recovery with reagentc and recreate it
Typical problems and remedies

Safe practice follows a fixed routine: verify the target by size and model, keep a current backup, disconnect drives that must not be touched, and prefer removable labels over remembered numbers. diskpart provides no undo, so a mistaken clean can only be reversed by partition-recovery software, and even that succeeds only before new data overwrites the disk.

In everyday language

diskpart is a text tool in Windows for dividing and preparing drives. Think of a disk as an empty warehouse. diskpart draws the walls between rooms, gives each room a name and a letter, and gets the shelves ready so files can be stored.

You first point at one disk or room, and then every command acts on that choice. That is efficient, but it never asks twice. Pointing at the wrong disk and typing one word can erase everything on it, so checking the selection carefully is the most important habit.

Seen in the real world

A desktop support technician at a mid-sized accounting office prepares a replacement 1 TB NVMe drive for a laptop. The new drive arrives with an old MBR layout from a previous test, and the company deployment requires UEFI boot with a GPT disk.

The technician boots the laptop from a Windows PE USB stick and runs list disk. Two disks appear, the 954 GB NVMe drive and the 29 GB USB stick, so the technician uses detail disk to confirm the model before acting. A prepared script then runs clean, convert gpt, and creates a 260 MB EFI System Partition, a 16 MB Microsoft Reserved Partition, a Windows partition and a 1 GB recovery partition.

The entire layout is ready in under two minutes, the image is applied with DISM, and the laptop boots normally. Because the script verifies the target first, the same procedure is reused safely on 40 further laptops during the hardware refresh.