All guides

Advanced DiskPart Commands: The Complete Guide to Formatting Disks & USB Drives

If you have ever fought with a USB drive that refuses to format, shows the wrong capacity, says "write-protected," or turns up as RAW, you already know the Windows Disk Management GUI has limits. That is where DiskPart comes in. It is the command-line disk partitioning tool built into every modern version of Windows, and it can do things the graphical tool simply cannot. In this guide you will learn the basics, the step-by-step method to format any disk or USB, and a collection of advanced and lesser-known commands (the "secret codes" that most users never discover).

Warning: DiskPart does exactly what you tell it to, instantly, with no "Are you sure?" prompt. Selecting the wrong disk will destroy your data. Always run "list disk" and double-check the disk number and size before running "clean" or "format".

1. How to Open DiskPart

  1. Press Win + S, type cmd.
  2. Right-click Command Prompt and choose Run as administrator.
  3. Type diskpart and press Enter.

You will see the prompt change to DISKPART>. To leave, type exit.

2. The Core Concept: Select First, Then Act

DiskPart works on a focus system. You must first select an object (disk, partition, or volume), and every command afterwards applies to that selected object.

list disk
select disk 1
list partition
select partition 1
list volume
select volume 3

The selected item is marked with an asterisk (*) in the list output. If you ever lose track, run "detail disk", "detail partition", or "detail volume" to see exactly what is selected.

3. Basic Method: Format a USB Drive or Disk

This is the standard, safe sequence that fixes most problems.

diskpart
list disk
select disk 1
clean
create partition primary
select partition 1
format fs=ntfs quick label="MyUSB"
assign
exit

What each line does:

  • "list disk" shows all connected drives with their size. Identify your USB by its capacity.
  • "select disk 1" focuses on that drive (replace 1 with your number).
  • "clean" wipes the partition table and all partitions.
  • "create partition primary" makes one partition using all available space.
  • "format fs=ntfs quick label="MyUSB"" formats it. Use "fs=fat32" or "fs=exfat" if needed.
  • "assign" gives it the next free drive letter automatically.

Which file system should you choose?

FAT32

  • Best for - Maximum compatibility (TVs, cars, consoles, BIOS/UEFI boot)
  • Limits - Max 4 GB per file; DiskPart's format limit is 32 GB volumes

exFAT

  • Best for - Large USB sticks and SD cards shared between Windows and Mac
  • Limits - Not supported by very old devices

NTFS

  • Best for - Windows drives, large files, permissions
  • Limits - Limited write support on macOS and some devices

4. Quick Format vs Full Format

By default, "format" without "quick" performs a full format, which scans every sector for bad blocks and can take a very long time on large drives. Use "quick" for speed. Use a full format only when you suspect the drive is failing.

format fs=ntfs quick
format fs=ntfs

Allocation unit size

You can set the cluster size with unit= :

format fs=ntfs unit=64k quick
format fs=exfat unit=128k quick

Larger units suit big files (video storage); smaller units waste less space on many tiny files.

5. The clean and clean all Difference

  • "clean" removes the partition and volume info only. It is fast, and the data could still be recovered with recovery software.
  • "clean all" writes zeros to every sector of the disk. It can take hours on a large drive, but it makes recovery of old data practically impossible. Use this before selling or giving away a drive.
select disk 2
clean all

6. Fixing Common Problems

Problem: "The disk is write-protected"

list disk
select disk 1
attributes disk clear readonly

Check whether the USB has a physical lock switch first. If the attribute clear works, the drive will accept writes again.

Problem: USB shows the wrong capacity (for example, 16 GB stick showing 2 MB)

This usually happens after writing a bootable ISO image directly to the drive. Fix it with:

select disk 1
clean
create partition primary
format fs=fat32 quick
assign

Problem: Drive appears as RAW or cannot be formatted from Explorer

select disk 1
clean
convert mbr
create partition primary
format fs=ntfs quick
assign

Problem: Drive is offline

select disk 1
online disk
attributes disk clear readonly

Problem: Drive letter is missing

select volume 3
assign letter=K

To remove a letter without deleting data:

select volume 3
remove letter=K

7. MBR vs GPT: Convert Between Partition Styles

  • MBR works with legacy BIOS and disks up to 2 TB.
  • GPT is required for UEFI boot on modern systems and for disks larger than 2 TB.
select disk 1
clean
convert gpt

or

select disk 1
clean
convert mbr

"convert" requires the disk to be empty, so run "clean" first (this erases data). Check the current style with "list disk" and look for an asterisk in the Gpt column.

8. Create a Bootable USB Drive with DiskPart

This is one of the most popular uses of DiskPart. The classic method for a Windows installation USB:

diskpart
list disk
select disk 1
clean
create partition primary
select partition 1
active
format fs=fat32 quick label="WINSETUP"
assign
exit

Then mount the Windows ISO (right-click, Mount) and copy all of its contents to the USB drive.

The 4 GB file problem

Newer Windows ISOs contain "install.wim", which can exceed 4 GB and will not fit on FAT32. You have two options:

1. Split the file so it fits on FAT32 (keeps UEFI boot working):

dism /Split-Image /ImageFile:D:\sources\install.wim 
/SWMFile:E:\sources\install.swm /FileSize:3800

(Replace D: with the mounted ISO and E: with your USB, and copy everything else normally, skipping the original install.wim.)

2. Use NTFS for the USB. Note that most UEFI firmware cannot boot directly from NTFS without special drivers, so FAT32 with the split image is usually the safer choice.

9. Advanced DiskPart Commands

These are the commands that separate beginners from power users.

Shrink and extend volumes

select volume 2
shrink querymax
shrink desired=10240
extend size=5120
extend
  • "shrink querymax" tells you the maximum amount you can reclaim.
  • "shrink desired=10240" shrinks by 10 GB (values are in MB).
  • "extend" with no parameter uses all contiguous free space.

Create multiple partitions on one disk

select disk 1
clean
create partition primary size=51200
format fs=ntfs quick label="Data"
assign letter=E
create partition primary
format fs=exfat quick label="Shared"
assign letter=F

Sizes are always in megabytes. Modern Windows 10 and 11 also allow multiple partitions on removable USB drives, which older versions did not.

Hide a partition (remove its drive letter)

select volume 4
remove letter=G

The data stays intact, but the partition no longer appears in File Explorer.

Set volume attributes

select volume 4
attributes volume set readonly
attributes volume set hidden
attributes volume set nodefaultdriveletter
attributes volume clear readonly
attributes volume

Change the disk's unique ID

Useful when two cloned disks have the same signature and Windows puts one offline:

select disk 1
uniqueid disk
uniqueid disk id=A1B2C3D4

Rescan and refresh

rescan

Forces DiskPart to re-detect drives, handy after hot-plugging an external disk.

See supported file systems

select volume 3
filesystems

Shows the current file system and which formats are supported for that volume.

Delete stubborn partitions

"override" forces deletion of protected partitions such as system or recovery partitions. Use with great care.

select partition 2
delete partition
delete partition override

10. Automatic Mounting and SAN Policy

automount
automount disable
automount enable
automount scrub

Disabling automount stops Windows from automatically mounting new volumes, which is a useful safety feature when working with forensic images or servers. automount scrub clears stale mount point entries.

san
san policy=OnlineAll

The SAN policy controls whether newly detected disks come online automatically. OnlineAll brings everything online (handy if an external disk keeps showing as offline).

11. Virtual Hard Disks (VHD/VHDX) with DiskPart

DiskPart can create and manage virtual disks without any third-party software.

create vdisk file="C:\VMs\test.vhdx" maximum=20480 type=expandable
select vdisk file="C:\VMs\test.vhdx"
attach vdisk
create partition primary
format fs=ntfs quick label="Virtual"
assign letter=V

To disconnect:

select vdisk file="C:\VMs\test.vhdx"
detach vdisk

Other useful vdisk commands:

list vdisk
expand vdisk maximum=40960
compact vdisk
merge vdisk depth=1

This is great for sandboxing, testing, or creating portable encrypted containers.

12. Automate with Scripts

You can save DiskPart commands in a text file and run them automatically.

Create format-usb.txt :

select disk 1
clean
create partition primary
format fs=ntfs quick label="AutoUSB"
assign
exit

Run it with:

diskpart /s format-usb.txt

This is how IT admins prepare dozens of drives or deploy Windows images at scale. Always test scripts on a spare drive first, because a script has no confirmation prompt.

13. The "Secret Codes": Partition Type IDs and Attributes

These are the lesser-known identifiers that control how Windows and firmware treat a partition. You set them with the set id command.

Common MBR partition type IDs

ID Meaning

07 NTFS / exFAT / HPFS

0B FAT32 (CHS)

0C FAT32 (LBA)

06 FAT16

27 Windows Recovery Environment

EF EFI system partition

83 Linux native

82 Linux swap

Example:
select partition 1
set id=0C

Common GPT partition type GUIDs

Purpose & GUID

EFI System Partition

C12A7328-F81F-11D2-BA4B-00A0C93EC93B

Microsoft Reserved (MSR)

E3C9E316-0B5C-4DB8-817D-F92DF00215AE

Basic Data (Windows data)

EBD0A0A2-B9E5-4433-87C0-68B6B72699C7

Windows Recovery

DE94BBA4-06D1-4D40-A16A-BFD50179D6AC
Example:
select partition 1
set id=c12a7328-f81f-11d2-ba4b-00a0c93ec93b

GPT attributes

select partition 1
gpt attributes=0x8000000000000001
  • 0x0000000000000001 marks the partition as required (the system will not let you delete it casually).
  • 0x8000000000000000 marks it as no default drive letter, so it will not auto-mount.
  • 0x8000000000000001 combines both. This is exactly what Windows uses to protect its recovery partition.

Special partition creation commands

create partition efi size=100
create partition msr size=16

These create an EFI System Partition and a Microsoft Reserved Partition, the building blocks of a manual UEFI Windows install layout.

14. Other Handy Disk Management Shortcuts

Not everything lives inside DiskPart. These Windows shortcuts are worth knowing:

What it opens or does / Command (Win + R or CMD)

Disk Management GUI

diskmgmt.msc

Computer Management (includes Disk Management)

compmgmt.msc

Optimize Drives (defrag and TRIM)

dfrgui

Disk Cleanup

cleanmgr

Fix file system errors and locate bad sectors on E:

chkdsk E: /f /r

Quick format a drive from regular CMD

format E: /fs:ntfs /q

List all drive letters

fsutil fsinfo drives

Show free and total space

fsutil volume diskfree C:

Manage volume mount points and GUIDs

mountvol

Quick disk health overview

wmic diskdrive get model,size,status

Rebuild boot files after a partition change

bcdboot C:\Windows

PowerShell alternatives

PowerShell offers a more modern approach to the same tasks:

Get-Disk
Get-Partition
Get-Volume
Clear-Disk -Number 1 -RemoveData -Confirm:$false
Initialize-Disk -Number 1 -PartitionStyle GPT
New-Partition -DiskNumber 1 -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "MyUSB"

That last one-liner creates a partition and formats it in a single step.

15. Quick Reference Cheat Sheet

Task / Command

List all disks

list disk

Select a disk

select disk N

Wipe partition table

clean

Wipe every sector

clean all

Create partition

create partition primary

Quick format NTFS

format fs=ntfs quick

Assign letter

assign letter=X

Make partition active

active


Convert to GPT

convert gpt


Convert to MBR

convert mbr


Clear read-only

attributes disk clear readonly


Bring disk online

online disk



Shrink volume

shrink desired=MB


Extend volume

extend

Run a script

diskpart /s file.txt

16. Safety Tips Before You Press Enter

  1. Always run "list disk" first and confirm the size of the target drive.
  2. Disconnect other external drives when working on a critical task.
  3. Back up your data. "clean" and "format" are not reversible in practice.
  4. Read the selected disk twice with "detail disk" before wiping.
  5. Use "clean all" when you are giving a drive away, and a quick "clean" otherwise.
  6. Test scripts on a spare USB before using them on real disks.

Final Thoughts

DiskPart looks intimidating at first, but once you understand the select-then-act pattern, it becomes one of the most powerful tools in your Windows toolkit. Whether you are rescuing a corrupted flash drive, building a bootable installer, setting up a multi-partition layout, or scripting disk preparation for a whole office, these commands give you precision and control the GUI cannot match. Bookmark this guide, and next time a USB drive misbehaves, you will know exactly what to type.

Found this useful? Share it, and follow madushka.com for more practical Windows, networking, and IT guides.