Diskpart.exe: The Windows Disk Partitioning Tool
Introduction:
diskpart.exe
is a powerful command-line utility included with Microsoft Windows operating systems (starting with Windows 2000 and later). It's a text-mode command interpreter that enables you to manage disks, partitions, and volumes from a command prompt or a script. It replaces the older fdisk
utility found in MS-DOS and earlier Windows versions. Diskpart is significantly more versatile and offers greater control over disk configurations. Crucially, it is a native Windows component; it is not a virus and cannot become a virus itself. However, misuse of diskpart
can lead to data loss, so caution is paramount.
Origin and Purpose:
- Origin: Developed by Microsoft as part of the Windows NT family of operating systems. It provides a command-line interface for the Disk Management service.
- Purpose:
diskpart.exe
is designed for:- Creating, deleting, and resizing partitions.
- Formatting volumes with different file systems (NTFS, FAT32, exFAT).
- Assigning or removing drive letters and mount points.
- Converting disks between basic and dynamic disk types.
- Converting disks between MBR (Master Boot Record) and GPT (GUID Partition Table) partition styles.
- Managing volumes (simple, spanned, striped, mirrored, RAID-5).
- Cleaning disks (removing all partitions and data).
- Creating virtual hard disks (VHDs).
- Offline/online disks and volumes.
- Extending volumes.
- Rescanning disks.
Is it a Virus? Is it Vulnerable?
- Virus Status:
diskpart.exe
is a legitimate Windows system file. It is not a virus. - Vulnerability:
diskpart.exe
itself is not inherently vulnerable to becoming a virus. However, malicious actors could potentially usediskpart
commands within a script or batch file to damage a system or erase data. The vulnerability lies in how it's used, not in the tool itself. This is why it is critical to only rundiskpart
commands from trusted sources and to understand the implications of each command before executing it. Never run scripts containingdiskpart
commands from untrusted websites or emails.
Detailed Usage Instructions:
1. Accessing Diskpart:
- Open Command Prompt (as an administrator): Search for "cmd", right-click "Command Prompt", and select "Run as administrator". This is essential, as
diskpart
requires administrative privileges to modify disk configurations. - Type
diskpart
and press Enter. The command prompt will change toDISKPART>
.
2. Common Commands (with examples and precautions):
list disk
: Displays a list of all disks connected to the computer. This is crucial for identifying the target disk before performing any operations. Note the disk numbers carefully!select disk <number>
: Selects the disk you want to work with. Replace<number>
with the actual disk number (e.g.,select disk 0
). Double-check this selection; operating on the wrong disk can lead to catastrophic data loss.list partition
: Lists the partitions on the selected disk.select partition <number>
: Selects a specific partition on the selected disk.detail disk
: Shows detailed information about the selected disk (type, status, size, etc.).detail partition
: Shows detailed information about the selected partition.create partition primary [size=<n>] [offset=<n>] [id={ <byte> | <GUID> }] [align=<n>] [noerr]
: Creates a primary partition.size=<n>
: Specifies the size of the partition in megabytes (MB). If no size is specified, the partition will use all available space.offset=<n>
: Specifies the offset (in KB) where the partition will start. Usually, you don't need to specify this manually.id={ <byte> | <GUID> }
: Sets the partition type ID. Usually, you don't need to specify this manually unless you're doing something very specific.align=<n>
: Aligns the partition to a specific boundary (in KB). This can improve performance on certain types of storage.noerr
: Continues even on error, for use in scripts. Example:create partition primary size=50000
(creates a 50GB primary partition).
create partition extended [size=<n>] [offset=<n>] [align=<n>] [noerr]
: Creates an extended partition. This is only needed on MBR disks to create more than four partitions. You can then create logical drives within the extended partition.create partition logical [size=<n>] [offset=<n>] [align=<n>] [noerr]
: Creates a logical drive within an extended partition.delete partition [noerr] [override]
: Deletes the selected partition. This is irreversible. All data on the partition will be lost. Theoverride
parameter is required to delete system, boot, or any partition containing the active paging file or crash dump (memory dump). Use with extreme caution.format fs=<filesystem> [quick] [label=<"label">] [unit=<size>] [compress] [override] [noerr]
: Formats the selected partition.fs=<filesystem>
: Specifies the file system (e.g.,fs=ntfs
,fs=fat32
,fs=exfat
).quick
: Performs a quick format (doesn't check for bad sectors).label=<"label">
: Assigns a volume label.unit=<size>
: Specifies the allocation unit size (in bytes). Usually, the default is best.compress
: Enables NTFS compression (only valid for NTFS).override
: Forces a dismount of the volume before formatting.noerr
: continues even on error, for use in scripts. Example:format fs=ntfs quick label="Data"
(performs a quick format with NTFS and sets the label to "Data").
assign [letter=<d> | mount=<path>] [noerr]
: Assigns a drive letter or mount point to the selected partition. Example:assign letter=E
.remove [letter=<d> | mount=<path> | all] [noerr]
: Removes a drive letter or mount point.clean [all]
: Removes all partition information from the selected disk. This is irreversible. All data on the disk will be lost. Theall
parameter specifies that each and every sector on the disk be zeroed, which completely deletes all data contained on the disk. This is a secure erase but takes a long time.convert basic [noerr]
: Converts a dynamic disk to a basic disk. All dynamic volumes on the disk must be deleted first.convert dynamic [noerr]
: Converts a basic disk to a dynamic disk.convert mbr [noerr]
: Converts an empty GPT disk to an MBR disk.convert gpt [noerr]
: Converts an empty MBR disk to a GPT disk.active
: Marks the selected partition as active (for MBR disks only). This is used to tell the BIOS which partition to boot from. Use with extreme caution; setting the wrong partition as active can make your system unbootable.inactive
: Marks the currently active MBR partition as inactive.extend [size=<n>] [disk=<n>] [noerr]
: Extends the selected volume into unallocated space.size=<n>
specifies the amount of space to add (in MB).disk=<n>
specifies the disk to extend onto (for dynamic volumes only).shrink [desired=<n>] [minimum=<n>] [nowait] [noerr]
: Reduces the size of the selected volume.online [noerr]
: Brings a disk or volume online.offline [noerr]
: Takes a disk or volume offline.rescan
: Rescans all I/O buses for new disks and volumes.exit
: Exits thediskpart
utility.help
: Displays a list of available commands. You can also typehelp <command>
for help on a specific command (e.g.,help create partition
).
3. Scripting:
You can create text files containing a sequence of diskpart
commands and then run them using the /s
switch. For example, create a file named script.txt
with the following content:
select disk 1
clean
create partition primary
format fs=ntfs quick
assign letter=F
exit
Then, from the command prompt (as administrator), run:
diskpart /s script.txt
This is extremely powerful but also very dangerous. Test your scripts thoroughly on a non-production system before running them on a live system.
4. Virtual Hard Disks (VHDs):
`diskpart` can also manage VHDs. Here are some relevant commands:
* `create vdisk file=<"path"> [maximum=<n>] [type={fixed | expandable}] [sd=<SDDL string>] [parent=<"path">] [source=<"path">]`
* `select vdisk file=<"path">`
* `attach vdisk [readonly] [noerr]`
* `detach vdisk [noerr]`
* `compact vdisk`
* `expand vdisk maximum=<n>`
* `merge vdisk depth=<n>`
**Example:**
```
create vdisk file="C:\MyVHDs\NewVHD.vhdx" maximum=50000 type=expandable
select vdisk file="C:\MyVHDs\NewVHD.vhdx"
attach vdisk
list disk (to find the newly attached disk)
select disk <number> (replace <number> with the disk number of the VHD)
create partition primary
format fs=ntfs quick label="MyVHD"
assign letter=V
exit
```
This creates an expandable VHDX file, attaches it, creates a partition on it, formats it, and assigns a drive letter.
Precautions:
- Always run
diskpart
as an administrator. - Double-check the disk number before performing any operations.
- Be extremely careful with the
clean
anddelete partition
commands. These are irreversible. - Back up your data before making any major changes to your disk configuration.
- Test scripts thoroughly on a non-production system first.
- Understand the implications of each command before executing it.
- When in doubt, do not proceed. Consult with a more experienced professional if needed.
Conclusion:
diskpart.exe
is a versatile and essential tool for managing disks and partitions in Windows. While it's not a virus and doesn't become one, its power demands careful and informed use. By understanding the commands and taking appropriate precautions, you can leverage diskpart
to effectively manage your storage infrastructure. However, its command-line nature and the potential for irreversible data loss mean it's best suited for experienced users and system administrators.