Command Prompt - 6 disk management commands you should know

One of the easiest and most straightforward ways to manage your disks is to use the Disk Management tool from Windows. But hey, Disk Management is a visual tool and some of us like command based interfaces a whole lot more. That’s why we thought it would be a great idea to see how several disk management actions can be done from the Command Prompt, using  DiskPart and other command-line tools. In this article you will learn how to create, format, delete, defragment and check your partitions for errors, directly from the Command Prompt. Let’s get started:

Important aspects to consider before moving forward

In order to perform any action presented in this article, you need to have administrative permissions and run the Command Prompt as administrator. Check out 7 Ways To Run Programs As Administrator In Windows to see how to run Command Prompt as administrator.

It’s also very important for you to acknowledge the fact that, if you misuse the commands listed in this article, you risk losing your data. Be careful and make a backup of your data before going ahead. If anything bad should happen, don't say we didn’t warn you! :)

1. How to create a partition from Command Prompt

First of all, open the Command Prompt. Then, type diskpart to access the Windows tool called Diskpart.

Command Prompt, cmd, disk, management, partitions, Windows

After you've launched diskpart it's time to establish which hard drive or solid state drive will be used to create a new partition. You will select the drive by using the select command. To view the available disks on your system, use the command list disk.

Command Prompt, cmd, disk, management, partitions, Windows

To select the disk you want to work with, type select disk followed by the number of the hard disk you want to use.

Command Prompt, cmd, disk, management, partitions, Windows

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 partition must be of a size equal or less to the amount of available free space.

For instance, in order to make a new primary partition with the size of 1000 MB, you should type “create partition primary size=1000”. Use the same command and change the size according to your needs and the available free space on your hard disk.

Command Prompt, cmd, disk, management, partitions, Windows

2. How to assign a letter to a partition from Command Prompt

You can and will probably want to also assign a letter to the newly created volume. 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 directly from Command Prompt, use the assign command.

For instance, if you wanted to assign the R letter to the partition you just created, you should type “assign letter = R”.

Command Prompt, cmd, disk, management, partitions, Windows

Please remember that before assigning a drive letter, you need to start Diskpart and select the disk for which you want to assign the letter.

3. How to format a partition from Command Prompt

Although you now have a new partition created on your computer or device and it also has a letter assigned so it’s easy to access it, you are still unable to use it until you also format that partition. To do that too, use the command “format fs=ntfs quick”. Use help format to find out more information about this command.

Command Prompt, cmd, disk, management, partitions, Windows

This command should be used after you start Diskpart and select the disk that you want to format.

When you are done working with diskpart, type the exit command to close the program.

Command Prompt, cmd, disk, management, partitions, Windows

4. How to delete a partition from Command Prompt

When you need to delete a drive from your computer, the procedure is quite simple. First, open the diskpart application. Then, select the partition that you want to delete and type the delete partition command. You can see an example of how the delete operation works, below. 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.

Command Prompt, cmd, disk, management, partitions, Windows

5. How to check a disk for errors from the Command Prompt

To check a disk for errors you have to use the chkdsk command. Type chkdsk followed by the drive’s letter. We recommend that you always use this command in combination with the /f parameter. This enables the tool to automatically fix any errors it finds.

As an example, we will scan the R: drive for errors by typing chkdsk /f r: and pressing Enter. The scan process will start.

Command Prompt, cmd, disk, management, partitions, Windows

After the scan is finished you see a report similar to the one from the picture below.

Command Prompt, cmd, disk, management, partitions, Windows

This summarizes 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.

6. How to defragment a disk from the Command Prompt

The Command Prompt also gives you a way of defragmenting a partition. All you have to do is type the command defrag followed by the drive’s letter. If you wanted to defragment the R: partition, for example, you would use “defrag r:”. If everything worked well, you should get a report like the one below, after the volume you selected has been defragmented.

Command Prompt, cmd, disk, management, partitions, Windows

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.

Conclusion

Despite the fact that you might think it's hard to manage your partitions through the Command Prompt, we hope that we managed to prove that it's not that difficult. The procedures involved are not complicated at all and, once you know the correct commands, using the Command Prompt can be a lot faster than using other tools like Disk Management. If you have any questions, feel free to leave a comment below.

About the Author: Codrut Neagu
Codrut is a Senior Editor on Digital Citizen. He's passionate about technology and he is fluent in working with several operating systems, including Windows and Linux. He likes to test security products and he feels like at home when digging through the registry, optimizing things or having fun on Telnet.