How to use disk management commands (diskpart, chkdsk, defrag) in Windows

tutorial
How to use disk management commands (diskpart, chkdsk, defrag) in Windows
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.
The Diskpart tool in PowerShell
The Diskpart tool in PowerShell 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:
diskpart
Launching Diskpart in Command Prompt
Launching Diskpart in Command Prompt 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:
list disk
Using Diskpart to list the drives from a PC
Using Diskpart to list the drives from a PC To select the disk you want to work with, type:
select disk x
…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:
select disk 3
Using Diskpart to select the disk on which the following commands are applied to
Using Diskpart to select the disk on which the following commands are applied to 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:
create partition primary size=10240
Using Diskpart to create a primary partition
Using Diskpart to create a primary partition 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:
assign letter = R
Using Diskpart to assign a letter to a partition (unhide a partition)
Using Diskpart to assign a letter to a partition (unhide a partition) 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:
format fs=ntfs quick
Formatting a partition with Diskpart in Command Prompt
Formatting a partition with Diskpart in Command Prompt 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:
delete partition
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.
Using Diskpart in Command Prompt to delete a partition
Using Diskpart in Command Prompt to delete a partition 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:
exit
Exiting Diskpart in Command Prompt
Exiting Diskpart in Command Prompt 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:
chkdsk
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:
chkdsk /f r:
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.
Using the Chkdsk command to check a drive for errors
Using the Chkdsk command to check a drive for errors 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:
defrag
…followed by the drive’s letter. For example, if you wanted to optimize the R: partition, you would use:
defrag r:
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.
Running defrag to trim an SSD or defragment an HDD
Running defrag to trim an SSD or defragment an HDD 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.
Discover: Productivity Apps CMD PowerShell Recommended System Tutorials Windows

Discussion (29)

  1. MIMI VACHER
    MIMI VACHER

    DIGITAL CITIZEN: PROBABLY THE BEST, MOST INFORMATIVE, CONCISE TUTORIAL EVER….NO PROBLEM WITH UNDERSTANDING THE CLEARLY WORDED STEP-BY-STEP INSTRUCTIONS!!!!!

  2. LINDA RODRIGUEZ
    LINDA RODRIGUEZ

    THIS WAS GREAT! HOW TO GET YOUR DISK OR PARTITION BACK. I JUST HAVE 232 GB OUR OF 1TB AND CAN NOT INSTALL WINDOWS 10

  3. Owner OF a DELL PC
    Owner OF a DELL PC

    this was so helpful. thank you! 🙂

  4. aravinth
    aravinth

    i tried everything and finally i used partition wizard tool and its worked for me

    1. Anonymous
      Anonymous

      It is good that you found a solution that worked. 😉

  5. Hamza
    Hamza

    Please tell the command for fregmentation of disk

  6. Yancy Willson
    Yancy Willson

    Ok, I followed instructions per your site. And may I say first that the hard drive that I am trying to fix came from a machine that I bought performance HDD for. So now I am using another machine and hdd dock station. All went well to the point when I tryed to delete the partition. This is what command propt tells me. (Virtual disk service error:
    Cannot delete a protected partition without the force protected parameter set.
    So how do I set the force protected parameter?
    If you know how and do not mind helping me learn more about my machine, I would be very gratful.

    1. Anonymous
      Anonymous

      Hi, try to use the “delete partition override” command instead.

  7. Mike
    Mike

    Great article. Love all the articles I read from D.C. One comment though: I would have mentioned, in the defrag section, that one should not defrag an SSD. This puts extra wear & tear on the drive and is actually unnecessary because of the speed of the drive. Keep up the good work & thank you.

  8. Amit singh
    Amit singh

    Nice Article

    1. Ciprian Adrian Rusen
      Ciprian Adrian Rusen

      Thanks!

  9. Diana Peek
    Diana Peek

    how to restore all disk partitions ! how to recover severel operating systems when disk partion cmd prompt cmd has been ussd by unotherized user left no disk keeping storage. on any letter

  10. Jun
    Jun

    How to format a write protected disk?

  11. abdullah
    abdullah

    will i lose my date after deleting volume

  12. paris
    paris

    i only have disk 0 and disk 1. disk 0 is 298gb but no free space and disk 1 is 0gb. I cant get windows os 8.1 to reinstall my system crashed. I need to create a partition I guess can u help?

  13. Elina
    Elina

    Im trying to recover data from my sd card that says that its damaged and needs to be reformatted. Looked up the solutions and it says to go thru Cmd and type in diskpart and then enter, then list disk. Well, the problem is, after i hit diskpart then enter, the next window comes up and doesn’t let me type anything. Not sure what to do at this point. Please, help!

  14. guna
    guna

    i have one doubt. In my laptop i have 1TB hard disk but i have one disk only. i do like this command partition means is their any chance to get the OS corrupt?

    1. Ciprian Adrian Rusen
      Ciprian Adrian Rusen

      If you work with the commands shared in this article and you want to re-partition your hard disk, make sure that your data is backed up so that you don’t lose it, in case something goes wrong.

  15. Barry Brian
    Barry Brian

    How to in large hard disk

  16. reza
    reza

    Hi, very useful information. Got a question though.
    when I use “list partition” I can see three partitions which I used to have. However, one of them on which I have my operating system is missing or somehow got inactivated, i.e when type “list volume” I am not able to see the driver or programs. How can I recover it or making it available again so that I can have my Pc work again?

    Tnx

  17. t0tti
    t0tti

    how can i do this commands by batch file?

  18. Avinash
    Avinash

    Hi… Your site is very good and the descriptions are very much easy and understandable… Hats off to you.

  19. Joshua
    Joshua

    Hi,i have a problem,when i try the list disk,the free space shown is 0…so what is the problem?

  20. Ledian
    Ledian

    Hi, this is deff helpful. However, I just build a barebones comp and when I give the LIST DISK command it tells me there are no fixed disks to show. I know my hdd is working properly, and the BIOS recognizes it too, but I’m stuck and don’t know what to do. Any help is much appreciated

  21. Dan Wasson
    Dan Wasson

    Hi this was great because I am currently stuck in command line, trying to back up key files from my c: drive before i do a factory restore of win7. (i’m here because I can’t get windows to boot so i’m going to nuke it). I’d like to make another partition so that I can put the backup files (like 10-20gb) onto that drive (like I’ll call it F:) so the windows restore doesnt blow it away.

    question – I got to the part where I was going to create a partition, but it says I have no free space.

    List Disk says 300gb hard drive has no free space (size: 298gb, free: 0b).
    List Volume says D: Recovery 14gb, C: OS 283gb

    but – when I look at the drive space actually available on C (c: dir) there’s about 180gb free.

    is there a way to use that 180gb of free space to create another partition/virtual drive with a different drive letter / mount?

    thanks!

    1. Ciprian Adrian Rusen
      Ciprian Adrian Rusen

      If Windows seems to be reporting your free space incorrectly, it is good to perform a scan disk and repair any errors on it. Info about this tool can be found here: https://www.digitalcitizen.life/test-partition-errors-check-disk

      Personally, I recommend using a more visual tool to manage your partitions and create new ones, like the Disk Management Tool: https://www.digitalcitizen.life/how-manage-your-disks-using-disk-management-utility

      The command line is trickier to use and more error prone if you don’t know very well what you are doing.

  22. adam
    adam

    what do you do if it says there are no fixed disks to show.

  23. Rizwan
    Rizwan

    i have a doubt that if i have to create a partition in GB scale then how many zeros must i add , i know the question is silly but i face a lot problems when i try to partition my disk using CMD

  24. kevin
    kevin

    How to resizing a partition using the command prompt? are there any article about this? thx