One of the easiest ways to manage your disks in Windows is by using the Disk Management tool. However, if you prefer command-line interfaces, you can perform several disk management tasks using Command Prompt or PowerShell. In this guide, I’ll show you how to use Diskpart and its commands to create, format, delete, hide, and unhide partitions. I’ll also show you how to use Chkdsk to check partitions for errors and Defrag to optimize them.
How to manage disks in CMD or Powershell
The terminal-based tool you can use to manage disks right from the command line is called Diskpart. It’s a command-line utility included by Windows 10 and Windows 11 that allows you to manage your computer's drives, including disks, partitions, volumes, or virtual hard disks. Diskpart can perform a variety of disk management tasks, such as creating and deleting partitions, formatting drives, and assigning drive letters.
You must run Command Prompt or PowerShell with administrative privileges to use Diskpart. You can also use Windows Terminal as an admin and open a Command Prompt or PowerShell tab in it. Once you have your favorite command-line tool running as an administrator, carefully read the next sections of this guide to learn the disk management commands and how to execute them. Also, be cautious: misusing these commands can result in data loss, so I strongly recommend backing up your data before making any changes. Having said all that, here’s how to use Diskpart in Windows:
How to create a partition with Diskpart
In Command Prompt or PowerShell, run the command:
After launching Diskpart, you need to establish which hard disk or solid state drive you want to use for creating a new partition. You can select the drive with the select command. Before that, however, you need to view the available disks on your system, so run this command to check the disk list in CMD:
To select the disk you want to work with, type:
…where x is the number of the hard disk you want to use. For example, to select the Disk 3 on my desktop PC, I run the command:
To create a new partition, you need to use the create partition command followed by the partition type and size. Note that the list command used previously also displays the available free space on each hard disk. Your new partition must be of a size equal to or less than the amount of available free space.
For instance, to make a new primary partition with the size of 10240 MB (10 GB), you must run the following disk management command:
You can use the same command and change the size according to your needs and the available free space on your hard disk.
How to assign a letter to a partition with Diskpart
You can and probably want to also assign a letter to the newly created partition. Assigning a letter to a partition makes it visible in File Explorer so that you can easily access it. To assign a letter to a partition, use the assign command. For instance, if you wanted to assign the R letter to the partition you just created, you should type:
Please remember that before assigning a drive letter, you need to start Diskpart and select the disk to which you want to assign the letter.
How to format a partition with Diskpart
You now have a new partition created on your computer or device and it also has a letter assigned. So it is easy to access it, but you are still unable to use it until you also format that partition. To do that too, use the command:
This command should be used after you start Diskpart and select the disk that you want to format.
TIP: You can use help format to find out more about this command.
How to delete a partition with Diskpart
When you need to delete a drive from your computer, the procedure is quite simple. First, open the Diskpart tool. Then, select the partition that you want to delete and type the command:
Below, you can see an example of how the delete operation works. It is a matter of selecting the disk where the partition is found, then selecting the volume to be deleted and running the delete command.
TIP: You can use the delete partition command in CMD to clean up an entire disk if that’s what you want. Just repeat the steps above for every partition on the drive.
How to exit Diskpart
When you are done working with Diskpart, you can close it by running the command:
Alternatively, you can simply close the Command Prompt, PowerShell, or Terminal window and be done.
NOTE: If you want to learn more about this powerful tool and see what else you can do with it, Microsoft offers excellent documentation here: Microsoft Learn Diskpart.
How to check a drive for errors with Chkdsk
To check a disk for errors, you can use the command:
Type chkdsk followed by the drive’s letter. I recommend that you always use this command in combination with the /f parameter. This enables the tool to automatically fix any errors it finds.
For example, to scan my R: drive for errors and have them fixed, I run the command like this:
The scan process starts immediately. After it’s done, you see a report similar to the one in the next screenshot, summarizing how much disk space the partition has, how many files are in use by the system, how much free space is available on the disk, how many bad sectors were identified and so on.
NOTE: If you’d like to learn more about the Chkdsk command and all its supported parameters, check Microsoft’s official documentation: Microsoft Learn Chkdsk.
Finally, you can also learn about using the Check Disk app from this tutorial on testing for hard disk errors, and fixing them.
How to optimize a drive with Defrag
Command Prompt and PowerShell also give you a way of optimizing (trimming or defragmenting) a partition. All you have to do is type the command:
…followed by the drive’s letter. For example, if you wanted to optimize the R: partition, you would use:
If everything works well, after the volume you selected has been optimized (trimmed if it is an SSD, or defragmented if it is a hard disk), you get a report like the one below. The report includes information prior to the optimization process and after it was completed, like the volume size, free space, total fragmented space, and the largest free space size.
NOTE: The Defrag command can offer many other options and switches that you can use to alter its behavior. To find them all, read its official documentation: Microsoft Learn Defrag.
Are you using CMD disk management commands?
Managing partitions through Command Prompt or PowerShell is straightforward once you know the correct commands. It can be quicker than using the standard Disk Management tool available in Windows. However, you need to pay attention, especially when using Diskpart so that you don’t damage your existing partition and lose precious data. If you have any questions, feel free to leave a comment below.