Diskpart program. Formatting and creating a bootable USB flash drive or disk via the command line

Diskpart is a tool for managing disk objects on a computer: disks, volumes, and partitions. It is a text-based command interpreter. Allows packages from a sequence of commands and saves them to automated scripts. It is part of the Windows family of operating systems, both home (starting from XP) and server (starting from 2003).

Running and entering commands

Command Usage and Scripting

Using the sequence of commands Diskpart, we can create, for example, a bootable USB flash drive:

Creating a bootable USB drive in the diskpart utility

Scripts are text files, the same sequence of commands is written on one line. The interpreter executes them in turn.

To run the script, enter the following at the command line: diskpart / s script_name.txt. Output from script execution can also be redirected: diskpart / s script.txt> log.txt.

If an error occurs while executing one of the script commands, the script will stop executing; to avoid this, add at the end of each script command:

Create partition primary noerr

format fs = FAT32 label = WinNT quick noerr

This option forces you to ignore errors and execute the script regardless of their occurrence.

DiskPart is a tool for working with disks, partitions and volumes in the Windows operating system. With DiskPart, you can perform important operations such as converting disk types, creating partitions and volumes, and configuring RAID arrays. In addition, DiskPart is used to configure automatic mounting of new disks in the file system, to assign drive letters and paths to mapped network drives. However, DiskPart is not designed to format drives. For this purpose, the FORMAT command is used.

Running the DISKPART utility

DiskPart works with physical hard drives installed in your computer. CD / DVD drives, removable media, or USB flash drives are not supported. Before using the DiskPart commands, you must enumerate and then select the disk, partition, or volume that you want to work with to give it focus. When a disk, partition, or volume is in focus, any Disk-Part commands affect that particular disk, partition, or volume.

Basic DISKPART Commands

The following commands are assigned to list the available disks, partitions and volumes:

  • list disk- Lists all the physical hard drives of the computer;
  • list volume- lists all disk volumes (including hard disk partitions and logical disks);
  • list partition- lists the partitions on the disk in focus.

When using the list commands, an asterisk (*) appears in focus next to a disk, volume, or partition. You select a disk, volume, or partition by its number or drive letter, for example disk 0, partition 1, volume 2, or volume D.

When you are finished with DiskPart, enter exit at the DiskPart command prompt to return to the standard command prompt.

Examples of using DiskPart

To understand how to work with DiskPart, consider the following example, where DiskPart starts, lists the available disks, and sets the focus to disk 2.

  1. To run DiskPart, enter diskpart at the command line.
  2. The command line will look like this: DISKPART>
  3. This indicates that the DiskPart interpreter is running. To list the available disks, enter list disk at its command line.
  4. The list contains available disks, their status, size and free space: Disk ## * Status Size Free Dyn GptDisk 0 Online 56 GB OB Disk 1 Online 29 GB OB Disk 2 Online 37 GB 33 GB
  5. Since you are going to work with disk 2, you must give it focus by entering the command select disk
  6. DiskPart will report: Disk 2 is now the selected disk
  7. When you are finished with the disk, enter exit at the command line to exit DiskPart.

Not all users know how to format a USB flash drive using the command line. Although this method is the most effective and convenient solution to quickly change the media format. Therefore, we want to tell you in detail about this operation, various accompanying nuances and features.

The formatting procedure may be needed in a variety of situations:

  1. Let's say you need to quickly delete all files stored on a USB flash drive.
  2. Another option - you want to completely clean the media from viruses and malicious software.
  3. Change the format of the file system. For example, from FAT32, where there are restrictions on the size of the files being written, to NTFS.
  4. Tired of the slow operation of the flash drive? Or does the device not see it, but only displays a window with errors when connecting? Formatting will again be useful and will most likely help fix the problem.
  5. Also, quite often users resort to this procedure if they need to fix errors on the drive that interfere with reading or writing files.
  6. If you need to create a bootable USB flash drive, then, again, you cannot do without formatting.

As you can see, there are many situations in which this operation is useful and irreplaceable. Therefore, it will be useful for you to learn how to format a USB flash drive through the command line.

Through the Format command

This is the most common way. With it, you formally do everything the same as if you were using standard Windows tools. But in this case, the command line is involved. At the same time, there is nothing complicated:


In addition, instead of the above command, you can enter another slightly modified one. It looks like this - "Format / FS: NTFS H". "N" - in this case, is the drive we need. If you need to change the file system to FAT32, then the command should look like this "Format / FS: FAT32 H".

For reference! If your flash drive does not work well, information is not read from it, and new data, on the contrary, is not written, then it remains to resort to at least low-level formatting. To do this, use a special utility, not the command line. For example, the HDD Low Level Format Tool program.

Using the "diskpart" utility

There is another way to start formatting the flash drive through the command line. In this case, a special utility built into the OS is used, which serves to manage disk space. It is called diskpart.

To use it to format the drive, do the following:


Now, after formatting via diskpart, the flash drive can be made bootable. You just need to copy the operating system distribution files to the media.

What else should the user know? Working in the command line with the built-in diskpart utility is recommended in cases where the USB flash drive cannot be formatted in a simple way from the explorer menu. If, when working with removable media, you notice that part of the file system is not visible or, for some unknown reason, its size has decreased, then it is also advisable to format it as described above.

Format the USB flash drive with the SDFormatter utility

In case the methods of formatting via the command line do not suit you (for example, they seem inconvenient), we can recommend the SDFormatter graphical utility. It contains only two formatting options, while allowing you to customize the cluster size. You can download the program here. The functionality is free.

Finally

Through the command line, you can format any USB media or hard drive without resorting to standard formatting tools or special utilities. At the same time, even a novice user can easily cope with this procedure. The main thing is to know the commands, as well as be able to enter them correctly and consistently.

One of the ways to format a USB flash drive is to use the command line. Usually it is used when it is impossible to do it by standard means, for example, because of an error that occurs. How formatting occurs through the command line, we will consider next.

We'll look at two approaches:

  • through the command "Format";
  • via utility "Diskpart".

Their difference is that the second option is addressed in more complex cases when the flash drive does not want to be formatted in any way.

Method 1: "format" command

Formally, you will do the same as in the case of standard formatting, but only using the command line.

The instruction in this case looks like this:


If an error occurs, you can try to do the same, but in "Safe mode"- so no extra processes will interfere with formatting.

Method 2: Utility "diskpart"

Diskpart is a special utility for managing disk space. Its wide functionality includes formatting the media.

To use this utility, do the following:



Thus, you can set all the necessary formatting settings for the flash drive. In this case, it is important not to mix up the drive letter or number, so as not to erase data from other media. In any case, it is not difficult to complete the task. The advantage of the command line is that all Windows users have this tool, without exception. If you have the opportunity to use special programs for removal, use one of those indicated in our lesson.

When trying to format a flash drive (less often a hard disk) using the "DiskPart" command interpreter, the user encounters the error "DiskPart has encountered an error", after which the reason for the error is often given (for example, "The media is write-protected"). In this article I will tell you about the most common errors in the operation of "Diskpart", as well as explain how to fix them.

DiskPart encountered an error (Access Denied)

Mistake # 1. The media is write protected

The user may come across the message "The medium is write-protected" when trying to format his flash drive, while the latter may not have a special jumper for write protection (if there is one, just try to switch it).

Solution # 1

  1. Run Command Prompt as Administrator, type there:
  2. DiskPart - and press enter.
  3. Then type: list disk press enter again.
  4. A list of disks available on the PC will be displayed, mark what letter the problem disk (flash drive) has.
  5. Type: select disk X - (replace X with the letter of the problem disk) and press enter.
  6. Type: attribute disk and hit enter.
  7. You will be shown information whether the problem disk has a "read only" status.

Here the flash drive has the status "Read only"

If so, type: attribute disk clear readonly and hit enter. This status will be changed to “No”. Try to format the desired drive again with DiskPart.

Solution # 2

Click on the "Start" button, in the search bar enter regedit and press enter. Follow the path:

HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ StorageDevicePolicies - and set the value of the "WriteProtect" parameter there to 0 (zero).

If you do not find the endpoint of the "StorageDevicePolicies" branch, then you need to create it. Right click on the previous step of the branch (Control) - "New" - "Section". Give the name to the section “ StorageDevicePolicies"(Without quotes).


Right click on the created section on the left - "Create" - "Parameter Dword (32 bits)". Rename the parameter to "WriteProtect" (without quotes). Set its value to "0", click on "OK", and close the system registry. Restart your PC and try running DiskPart again.

Mistake # 2. Access denied

DiskPart encountered an access denied error usually occurs when you try to clean up your USB flash drive using the "clean" diskpart command. In the error message, the system recommends viewing the logs, which can detail the cause of the problem.

Solution # 1

View the system logs (Control Panel - System and Security - Administrative Tools - View Events - Windows Logs - System). Perhaps the cause of the dysfunction will be indicated there, you can identify and correct it.

Solution # 2

Run the command line only as administrator, and already in it enter the command "diskpart".

Mistake number 3. DiskPart encountered an error parameter is set incorrectly

The error parameter is set incorrectly usually occurs in a situation when the file structure of the disk is damaged or the encryption driver of the disk in Windows OS has been changed.

Solution # 1

Go to "My Computer", hover over the problem disk, right-click, select "Properties" from the menu that appears. In the menu that opens, go to the "Service" tab - "Check". Check both boxes and click on "Run".

Perform disk check with CHKDSK

Solution # 2

Download, install, and run the "Partition Guru" program. Click on the problem disk, then click on the "File Recovery" button, and then on the "Start" button. Wait for the end of the file recovery process.


Use Partition Guru to check your disk

Mistake # 4. CRC data error

It occurs due to the presence of bad (bad) sectors on the device.

Solution

Use CKDSK as described above, or run "Partition Guru", select the problem disk, select the "Disk" menu section, and in it "Verify Or Repair Bad Sectors" - "Start Verify". When the check is over, click on "Repair".


Check for bad sectors on the disk using "Partition Guru"

Mistake # 5. The request was not fulfilled

Usually indicates a hardware problem with the drive itself.

Solution

  1. If you have such problems with a flash drive, then go to Device Manager (click on the "Start" button, in the search bar enter devmgmt.msc and press enter).
  2. Find "USB Controllers" there, open a subsection by clicking on it, and uninstall all its contents (click on the first subdevice in the queue, press the right mouse button - Delete, and so on until you delete all the contents of "USB Controllers").
  3. Then reboot your computer, and try again to start DiskPart and do it.
  4. If it does not help, then most likely your flash drive is hardware defective.

Mistake # 6. Device I / O error

A device I / O error occurs when a device (flash drive or hard disk) cannot write and read data.

Solution

Check the hardware connection between the device and the PC. If this is a USB flash drive, use another USB connector, if a hard drive, check the cables and plugs, as well as the tightness of the connection of the latter to the corresponding connectors.

Conclusion

The appearance of the message "DiskPart has encountered an error" can be caused by a number of reasons, most of which I have listed above. Concretize the problem and then follow the steps I indicated to fix the "DiskPart has encountered an error" on your PC.