Build diskpart scripts to list, clean, partition and format disks safely.
diskpart is the most destructive command-line tool shipped with Windows. There is no confirmation prompt, no undo, and no recycle bin. clean takes effect the instant you press Enter, and it acts on whichever disk you last selected — which is not necessarily the one you meant. This builder writes the script for you, and deliberately puts list disk at the top of every sequence so you have the chance to check.
Read that again before continuing: selecting the wrong disk number and running clean destroys the partition table of a disk you may still need. People do this to their backup drive, to a second data drive, and occasionally to the drive they are booted from. No warning appears first.
Every destructive sequence this builder produces starts the same way:
diskpartlist disk
followed by a REM line telling you to confirm the disk by its size. REM is a genuine diskpart command — it is how diskpart scripts carry comments — so you can paste the whole block without the comment lines causing an error.
Confirm the target with two facts, not one:
select disk 2 then detail disk. That prints the model, whether it is the boot disk, and the volumes and drive letters it contains. A drive letter you recognise is the clearest confirmation available. The builder does not emit detail disk for you — type it yourself, every time, before anything destructive.Disk numbers are assigned at enumeration and are not stable. Unplug a USB drive, reboot, or add a disk, and the numbering can shift. A disk number you noted yesterday is not a disk number you can trust today. Disk 0 is usually the boot disk, but “usually” is doing real work in that sentence — check detail disk for the Boot Disk field rather than assuming.
These sound like the same command with more thoroughness. They are not remotely the same operation.
clean | clean all | |
|---|---|---|
| What it writes | Wipes the partition table and volume metadata | Writes zeros to every sector on the disk |
| Time taken | Effectively instant | Hours on a large drive — it must write the whole surface |
| Is the data really gone? | No. The file contents remain on the platters or flash; only the map is destroyed. Recovery software can often rebuild a great deal of it | Yes, for practical purposes — every sector has been overwritten |
| Right choice for | Repartitioning a disk you are keeping; clearing a disk that will not accept a new layout | Disposing of, selling or returning a drive that held data you care about |
The distinction matters in both directions. If you are repurposing a disk, clean all wastes hours for nothing. If you are handing a drive to someone else, clean gives you a false sense of security — the data is still there. The builder has a checkbox for this precisely because choosing wrongly is costly either way.
One more wrinkle for solid-state drives: overwriting every sector on an SSD is neither guaranteed to reach every physical cell nor kind to the drive’s endurance, because of wear levelling and over-provisioning. For SSD sanitisation, the manufacturer’s secure-erase utility or a BitLocker crypto-erase is the better route. clean all is a reasonable answer for spinning disks.
| Task | Script it produces | Risk |
|---|---|---|
| List disks & volumes | list disk, list volume, list partition | None — read-only. Always start here |
| Clean a disk | select disk N, then clean or clean all | Destroys the entire disk |
| Create partition | select disk N, create partition primary, format fs=… quick, assign letter=… | Formats what it creates |
| Format a volume | list volume, select volume N, format fs=… quick | Wipes that volume |
| Assign drive letter | select volume N, assign letter=… | Low, but changing a letter breaks paths and shortcuts pointing at it |
| Convert GPT / MBR | select disk N, clean, convert gpt or convert mbr | Destroys the entire disk — see below |
| Set partition active | select disk N, list partition, select partition 1, active | Boot behaviour only, MBR disks only |
Two of these emit a literal N that you must replace: the format and assign tasks say select volume N, because the volume number can only be read off your own list volume output. Pasting the script unedited will simply fail with an invalid-argument error rather than acting on the wrong volume — which is the safer failure, but it does mean you cannot run those two blind.
This surprises people, so it is worth stating plainly. convert gpt and convert mbr only work on a disk with no partitions and no volumes. That is why the builder inserts clean before the conversion: without it, diskpart returns The specified disk is not convertible and nothing happens. With it, the disk is wiped and then converted.
If your actual goal is to convert a Windows installation from MBR to GPT so you can enable UEFI boot, diskpart is the wrong tool — mbr2gpt.exe, also built into Windows, does that conversion in place without destroying data. Use the diskpart route only for a disk whose contents you intend to lose.
Which style to pick for a fresh disk:
| Option | Use it for | Limits to know |
|---|---|---|
fs=ntfs | Internal Windows drives and any volume needing permissions | Read-only on macOS without third-party software |
fs=exfat | External drives shared between Windows and macOS, and large media cards | No permissions and no journalling — more vulnerable to corruption from a yanked cable |
fs=fat32 | Small removable media, and firmware or devices that only read FAT32 | Maximum file size 4 GB, and Windows will not create a FAT32 volume larger than 32 GB through diskpart |
The builder appends quick to every format. A quick format writes new file-system structures without checking the surface for bad sectors; a full format reads every sector as it goes. Quick is right almost always. Drop the keyword when you suspect the drive is physically failing and want the format to surface that.
select disk N again.active on a GPT disk. The active-partition flag is an MBR concept and has no meaning under GPT, where boot is handled by the EFI system partition.A short discipline that prevents nearly every disaster in this area: unplug every drive you are not working on. If the only external disk attached is the one you intend to wipe, a mis-typed disk number has far fewer places to land. Then run list disk, then select disk N, then detail disk, and only then the destructive line.
This builder runs entirely in your browser and only produces text. It has no access to your disks, cannot see your drive layout, and cannot verify that the disk number you typed is the one you meant. That verification is yours to do, and it is the only thing standing between a routine repartition and a data-loss incident.
diskpart accepts a script file, which is how these sequences get used in deployment and imaging work:
diskpart /s C:\scripts\prepare-disk.txt
The file contains the same lines the builder produces, one per line, without the leading diskpart. Everything runs unattended, and REM lines are ignored as comments. Add > C:\logs\diskpart.log to capture the output, because otherwise a failure mid-script leaves you with no record of which line stopped it.
This is also where the danger multiplies. A script has no pause at which you can look at list disk and change your mind — it selects a hard-coded disk number and acts. If you script anything destructive, script it against a machine whose disk layout you control completely, and put the list disk and detail disk output into the log so that after the fact you can prove what was attached at the time. For interactive troubleshooting, do not script it. Type the lines and look at the output between them.
The other place these commands turn up is Windows Recovery Environment, reached from a boot medium via Shift+F10 to get a command prompt. diskpart works there, and it is often the only way to touch a disk that Windows will not release while running. Be aware that drive letters in the recovery environment frequently differ from the ones the installed system uses, so C: may not be the volume you think it is — another reason to identify volumes by size and label with list volume rather than by letter.
Open diskpart as Administrator, run list disk and confirm the target by its size, then select disk N and clean. The clean command removes all partitions and volume information. Use clean all to zero every sector for a full, irreversible erase, which is slower.
clean operates on a whole disk and removes its entire partition layout, leaving it unallocated. format operates on a single selected volume and writes a fresh file system to it. clean wipes the structure of the disk, while format prepares one volume for use.
The disk must be empty first. Select it with select disk N, run clean to remove all partitions, then run convert gpt or convert mbr. Converting destroys existing data, so back up anything important before you start.
Always run list disk before any destructive command and identify the target by its capacity, not just its number, because disk numbers can shift between reboots. The select then act model means every command hits the last selected disk, so confirm the selection each time.