Diskpart commands delete partitions. Convert a basic disk to an active one via DiskPart. Previous Windows DiskPart Commands

One of the basic principles followed Windows developers Server 2008, is the ability to manage many components of the operating system from command line... If you need to create RAID array This is usually easiest to do using the Disk Management Console, but this is assuming you need to do this on one machine and that machine has a GUI interface. If you need to perform these actions on multiple machines, it is better to write a script (script) for these purposes. Also, if you are using Server Core 2008 in your environment, you may have no choice but to manage disk subsystem from the command line.

Diskpart Command

Windows Server 2008 includes the Diskpart.exe command-line utility that allows you to manage the server's disk subsystem from the command line. To use this command, just open a window command line and enter the command DiskPart.exe... After that, you will be taken to the command shell Diskpart. From here, you can run various commands to manage the disk subsystem.

Diskpart command syntax

A typical Diskpart command consists of a verb and a noun. The verb tells you what action to take, and the noun indicates the object with which you want to perform the action. One of the simplest examples of commands Diskpart is the team ListDisk... In this example, List is a verb and Disk is a noun. After entering this command, Windows will display a list of all physical disks installed on the server.

Pay attention to the above figure, you can see that each disk is assigned a number. If you need to perform operations on a specific drive, you must specify the drive by entering the Select command. For example, to perform an operation on disk 0, you would type Select Disk 0.

In many cases, disk management involves creating fault-tolerant volumes, and you can easily create them using the Diskpart command. For example, your system has 4 hard drives, and these hard drives are numbered 0, 1, 2, and 3. We will also assume that drive 0 is our system disk and disks 1, 2, and 3 are empty hard disks that we want to turn into a RAID 5 volume.

Before we can create a RAID 5 volume, we need to make sure that each of the disks is mapped as a dynamic disk. If you look at the figure, you can see that the List Disk command displays whether the disk is dynamic or not. Assuming none of the disks are dynamic, you can convert them from primary to dynamic by entering the following commands:

Select Disk 1 Convert Dynamic Select Disk 2 Convert Dynamic Select Disk 3 Convert Dynamic

Now that we have converted our disks, we can create a RAID 5 volume by entering the following commands:

Select Disk 1 Create Volume RAID Disk 1, 2, 3

Entering the List Volume command will verify that the RAID volume has been created. Please note that each volume is assigned a number.

The last thing we need to do is format the volume and assign a letter to it. This can be accomplished by typing the following commands:

Select volume 2 Format FS = NTFS Label = MyNewVolume Assign Letter = F

Using the command ListVolume, you can make sure that the volume is formatted and assigned a letter. Finally, enter the command Exit, to exit the Diskpart shell.

On computers with x86 processors, the MBR partition can be marked as active via the command line utility Diskpart. This means that the computer will boot from this section. You cannot mark volumes on a dynamic disk as active. When converting a basic disk with an active partition to a dynamic disk, that partition automatically becomes a simple active volume.

To make a partition active, follow the procedure below.

  1. Start DiskPart by entering diskpart at the command line.
  2. Select the drive containing the partition you want to make active, like this: DISKPART> select disk 0
  3. List the disk partitions with the command list partition.
  4. Select the required section: DISKPART> select partition 0
  5. Make the selected section active by entering the command active.

Change Disk Type in DiskPart

Windows XP and Windows Server 2003 support basic and dynamic disks. Sometimes it becomes necessary to convert one type of disk to another, and Windows provides the means to accomplish this task. When converting a basic disk to dynamic, partitions are automatically converted to volumes of the appropriate type. However, converting volumes back to partitions on a basic disk cannot be done just like that. First, you need to delete the volumes of a dynamic disk and only then convert it back to basic. Deleting volumes will result in the loss of all information on the disk.

Converting a basic disk to a dynamic one is an elementary process, but it imposes some limitations. Please consider the following considerations before starting this operation.

  • Only computers running on dynamic disks work with Windows control 2000, Windows XP, or Windows Server 2003. Therefore, if the disk to be converted contains earlier Windows versions, you will not be able to download these versions after conversion.
  • Disks with MBR partitions must have at least 1 MB free space at the end of the disc. Otherwise, the conversion will fail. Disk Management Console and DiskPart reserve this space automatically; however, when using other utilities to work with disks, you yourself have to worry about the availability of this free space.
  • Disks with GPT partitions must have contiguous, recognized data partitions. If a GPT disk contains partitions that are not recognized by Windows, for example, those created by another operating system, it will not be possible to convert the disk to dynamic.

In addition to the above, the following is true for any type of disc:

  • disks with sectors larger than 512 bytes cannot be converted. If larger sectors are used, the disk must be reformatted;
  • dynamic disks cannot be created on portable computers or removable media. In this case, disks can only be basic with primary partitions;
  • you cannot perform disk conversion if the system or boot partition is part of a mirrored, overlapped, striped, or RAID-5 volume. You must first undo the overlap, mirroring, or interleaving;
  • however, you can convert disks with other types of volumes that are part of mirrored, overlapped / or striped volumes, as well as RAID-5 volumes. These volumes become dynamic of the same type, and you must convert all disks in the set.

Convert a basic disk to dynamic in DiskPart

Converting a basic disk to dynamic is performed in the following sequence.

  1. Start DiskPart by entering diskpart at the command line.
  2. Select the drive to convert, for example: DISKPART> select disk 0
  3. Convert the disk by entering the command convert dynamic.

DiskPart is a tool for working with disks, partitions and volumes in operating system Windows. 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 partitions hard disk and logical drives);
  • 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.

For various tasks that users face, for example, you need to create bootable USB drive or there were problems with formatting the flash drive by standard means from Explorer, it becomes necessary to format the flash drive using the command line. Formatting a flash drive through the command line can be done in several ways.

In order to call the command line, launch the Start menu and enter the cmd command in the search bar.

In the command line window that appears, enter: format / fs: NTFS H: / q - where:

  • format - formatting task;
  • fs: NTFS - description of the chosen file system;
  • H: - the drive we need;
  • / q - command for quick format.

In the event that we want to format the file system in Fat or Fat32, the command will look like this: format / FS: FAT32 H: / q.

After entering the command, a message will be displayed: “Insert new disc into drive H: and press the ENTER key… ”- press ENTER.

Then the command line window displays: "Volume label (11 characters, ENTER - no label needed)" -

so press ENTER.

Our flash drive is formatted.

Format command (second way)

We call the command line as described in the first paragraph.

We type in the command line window that appears: format H: / fs: NTFS / v: Arhiv - where:

  • format - a task to format the disk;
  • fs: NTFS - description of the file system we choose;
  • v: Arhiv - the label of the drive we select (the name of your disk fits in).

Accordingly, when we choose another file system after fs: we enter what we need - Fat or Fat32. The command looks like this: format H: / fs: FAT32 / v: Arhiv. If you want to choose quick formatting, add Q to the formatting command and the command will look like this: format H: / FS: NTFS / Q / v: arhiv.

Immediately after entering the command, a notification appears in the command line window: "Insert a new disk into drive H: and press the ENTER key ..." - press the Enter key.

The flash drive is formatted.

Method 3. Built-in Diskpart utility

In the operating room Windows system there is a built-in utility for working with drives, which allows us to format the USB flash drive.

We call the command line with the cmd command in the search bar of the Start menu.

In the window of the command line that appears, enter: diskpart and the utility for managing storage spaces is launched.

We type the command: list disk. This will allow us to see all the drives present on our computer. We find a USB flash drive that we are going to format by volume. We can see all the disks with an indication of their volumes. We remember the number of the disc we select, for example, 2.

Then we type the command: select disk 2, where 2 is the drive we select. We press Enter.

After that, the flash drive must be cleared of attributes, for which we enter the command: attributes disk clear readonly. After that, enter the command: clean.

After clearing the drive from the attributes, we need to create a primary partition, for which we mark our disk in the file system we have chosen:

First, enter the command: create partition primary, then set the file system we need with the command: format fs = ntfs or format fs = fat32. If you need fast formatting, then write the command as follows: format fs = NTFS QUICK or format fs = FAT32 QUICK. Click on Enter and the flash drive is formatted.

Exit the program with the command: exit.

Built-in Diskpart utility (another way)

There is another, slightly different, way to format the drive using the built-in Diskpart program.

We open a command prompt window, as described above, then enter the diskpart command and press Enter to launch the utility.

Then enter the command list disk and press Enter again. This is how we will see all our drives. After that, just as in the previous way, we recognize our flash drive by its size and remember the drive number. For example, 2.

We write the command: select disk 2, where 2 is the flash drive we select. Click on Enter.

We enter the clean command and press Enter - all files on the drive are deleted.

Next, you need to create a new partition on the flash drive, for which the command is entered: create partition primary and Enter, then the command for selecting a disk: select disk 2 and Enter, where 2 is the drive we need. After that, you need to enter the command: active for the utility to mark the partition as active. Then we enter the command to markup the file system: format fs = ntfs or format fs = fat32. As mentioned in the previous method, for quick formatting, add to the QUICK command: format fs = NTFS QUICK or format fs = FAT32 QUICK.

After the end of formatting, you need to assign a letter to the USB flash drive. We do this using the command: assign, after which the drive starts up automatically, and we see an explorer window with an already formatted flash drive on the screen.

To exit Diskpart, use the exit command.

Conclusion

As we have shown in this article, it is not difficult to format a USB flash drive via the command line. The main thing is to carefully choose the disk for formatting and remember that after formatting your data may be lost forever. Working in the command line will help especially in the case of working with the built-in Diskpart utility, if the USB flash drive cannot be formatted in a simple way from the explorer menu or when working with a flash drive, you noticed that part of the file system is not visible and the size of the flash drive has decreased for some reason.

LIST and SELECT are just two of the many commands supported by DiskPart. Many of the above commands take Noerr as an optional parameter. The Noerr parameter is used with DiskPart scripts to instruct DiskPart to continue processing script commands when an error occurs. In the absence of this parameter, DiskPart will exit on error and terminate the script execution.

So, the main DISKART commands are:

  • Commands with which Noerr is used and which return an error code on shutdown: ADD, ASSIGN, AUTOMOUNT, BREAK, CONVERT, CREATE, DELETE, EXTEND, IMPORT, ONLINE, REMOVE and REPAIR.
  • Commands with which Noerr is not used or which do not return an error code: ACTIVE, CLEAN, DETAIL, EXIT, GPT, HELP, INACTIVE, LIST, REM, RESCAN, RETAIN and SELECT.

Using DiskPart Scripts

The way of using scripts with DiskPart is slightly different from that for other commands. The reason is that DiskPart is a command line interpreter and not a regular utility. When you start DiskPart (by typing diskpart at the command line), the interpreter needs to be told that you want to use the script by adding the / S parameter, as shown below:

  • diskpart / s ScriptName.txt

where ScriptName.txt is the name of the text file with the required script. By default, DiskPart writes output to the current command line.

Check the following error codes when running DiskPart scripts:

  • 0 - no errors, the script is fully executed;
  • 1 - fatal exception (possibly serious problems);
  • 2 - incorrect command parameters;
  • 3 - the specified script file or output file could not be opened;
  • 4 - the service used by DiskPart returned an error code or reported a failure;
  • 5 - incorrect command syntax (usually due to the wrong choice of disk, partition or volume, or because it cannot be used with this command).

Sample DiskPart Script

Using DiskPart scripts, all the operations you want to perform must be completed in a single session. The script should contain all the DiskPart commands you need. There is no need to include the EXIT command in the script, because the interpreter exits automatically at the end of the script.

  • rem Select disk 2
  • select disk 2
  • rem Create a primary partition on disk
  • rem and assign it a drive letter
  • create partition primary size = 4096assign letter = s
  • rem Create an extended section
  • rem with two logical drives
  • create partition extended size = 4096
  • create partition logical size = 2048
  • assign letter = u
  • create partition logical size = 2047
  • assign letter = v

This example creates a primary and secondary partitions on disk 2. The primary partition size is specified at 4096 MB and is assigned the letter S. The size of the secondary partition is specified at 4096 MB; it creates two logical partitions: the first is 2048 MB in size with the letter U, the second is 2047 MB ​​in size with the letter V. These sizes of logical partitions are set due to some loss of disk space during partitioning. You could also create a single 4096 MB logical partition.