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.
| Object | Selection command | Typical actions |
|---|---|---|
| Disk | select disk 0 | clean, convert gpt, online, attributes |
| Partition | select partition 2 | create, delete, format, set id |
| Volume | select volume C | assign letter, extend, shrink, format |
| Virtual disk | select vdisk file=path | attach, detach, compact, expand |
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.
| Command | Effect | Risk level |
|---|---|---|
list disk | Shows all disks with size and free space | None |
clean | Removes the partition table from the selected disk | Very high |
clean all | Writes zeros to every sector of the disk | Very high |
convert gpt | Changes an empty disk to GUID Partition Table | High |
create partition primary | Creates a partition in free space | Medium |
format fs=ntfs quick | Creates a file system on the partition | High |
assign letter=E | Gives the volume a drive letter | Low |
extend / shrink | Changes volume size into adjacent space | Medium |
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 compared with related tools
diskpart sits between the graphical console and the modern Storage module cmdlets in PowerShell. The comparison below shows when each tool fits best.
| Tool | Interface | Strengths | Weaknesses |
|---|---|---|---|
| diskpart | Interactive text or script | Available in Windows PE, low dependencies | Text output, no object pipeline |
| Storage cmdlets | PowerShell | Objects, filtering, remote use | Not present in minimal Windows PE |
| Disk Management | Graphical console | Visual overview, fewer typing errors | Not scriptable, limited operations |
| DISM | Command line | Applies and services images | Does not partition disks |
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.
| Property | MBR | GPT |
|---|---|---|
| Maximum primary partitions | 4, or 3 plus an extended partition | 128 by default on Windows |
| Maximum addressable size | About 2 TiB with 512-byte sectors | Far beyond current disk sizes |
| Boot firmware | Legacy BIOS | UEFI |
| Redundant partition table | No | Yes, primary and backup header |
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.
| Symptom | Cause | Remedy |
|---|---|---|
| Data disk wiped instead of USB drive | Disk numbers assumed, not verified | Check size and model with detail disk first |
| Extend fails with an error | Free space is not adjacent to the volume | Move or delete the blocking partition, or use cmdlets |
| Media are write-protected | Read-only attribute set on the disk | Run attributes disk clear readonly |
| New disk not visible in Explorer | Disk offline or uninitialized | Run online disk, then create and assign |
| Recovery partition blocks extension | Partition sits after the Windows volume | Relocate recovery with reagentc and recreate it |
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.