How To Format Drive Using Command Line In Windows

There are several ways to format a USB drive on a Windows computer. Some offer a Graphical User…

Format Disk using Command Line

There are several ways to format a USB drive on a Windows computer. Some offer a Graphical User Interface (GUI) while other methods can be applied using the command-line tools which the operating system offers.

In this article, we will discuss how to properly format a USB drive using Windows PowerShell and the Command Prompt.

These methods can come in handy if you are a command-line fanatic and want to save time by formatting a USB drive in a few easy commands, rather than having to navigate through several windows to get the job done.

How to Format Drive using PowerShell

Create Single Partition

Windows PowerShell is a powerful command-line tool in Windows that allows you to perform administrative tasks on your computer. Follow the steps given below to format a USB drive using this tool and create a single partition.

Note: This method works regardless of the drive letter being assigned to the USB drive.

  1. Plug the USB device onto your computer.
  2. Now open PowerShell with administrative rights and paste the following. This will give you the number associated with the USB flash drive, which will be used in the steps below.
    Get-Disk
    pwsh get disk
  3. Now use the following cmdlet to erase all data from the drive, while replacing Number with the number associated with the USB drive noted in step 1 above.
    Clear-Disk -Number <em>Number</em> -RemoveData
    pwsh remove data
  4. When asked for a confirmation, type in A and hit Enter.
    pwsh A
  5. Now use the following cmdlet to create a single, new partition consisting of the maximum usable space on the drive. Replace Number with the number associated with the drive noted in step 1, and Label with the new label which you want to give the new volume.
    New-partition -DiskNumber <em>Number</em> -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel <em>Label</em>
    pwsh vol
  6. The drive has now been formatted. Now assign a drive letter with the cmdlet given below, replacing the number with the disk number listed in step 1 above, and DriveLetter, which you want to assign to this USB.
    Note: Ensure the drive letter is not being used by another volume on the computer.
    Get-Partition -DiskNumber <em>Number</em> | Set-Partition -NewDriveLetter <em>DriveLetter</em>
    pwsh drive letter

After entering the last command, the USB drive will be accessible and will automatically open in Windows Explorer.

Create Multiple Partitions

You can also create multiple partitions of a USB drive using PowerShell, rather than just a single big partition. Here is how to create multiple partitions of a USB drive using PowerShell:

  1. Open PowerShell with administrative rights and paste the following. This will give you the number associated with the USB flash drive, which will be used in the steps below.
    Get-Disk
    pwsh get disk
  2. Now use the following cmdlet to erase all data from the drive while replacing Number with the number associated with the USB drive noted in step 1 above.
    Clear-Disk -Number <em>Number</em> -RemoveData
    pwsh remove data
  3. When asked for a confirmation, type in A and hit Enter.
    pwsh A
  4. Now use the command below to create a new parition of a specific size, format it, and assign a drive letter, all in one cmdlet. Replace Number with the number associated with the USB drive noted in step 1 above, Size with the size of the partition that you want to create, DriveLetter with the letter that you want to assign, and Label with the label that you want to assign to this partition.
    Note: Ensure that the drive letter is not being used by another volume on the computer.
    New-Partition -DiskNumber <em>Number</em> -Size <em>Size</em>gb -DriveLetter <em>DriveLetter</em> | Format-Volume -FileSystem NTFS -NewFileSystemLabel <em>Label</em>
    pwsh vol 2
  5. Repeat step 4 to create additional partitions in the USB drive.
    Note: The combined space of the partitions cannot exceed the total space available on the drive.
  6. At the end, create a volume of all remaining space on the drive using the given cmdlet:
    New-Partition -DiskNumber <em>Number</em> -Size $MaxSize -DriveLetter <em>DriveLetter</em> | Format-Volume -FileSystem NTFS -NewFileSystemLabel <em>Label</em>
    pshhh

You will now have to create at least 2 partitions on the drive using the steps given above.

We would like to highlight that you may be prompted with a message stating the following as you are performing steps 4, 5, or 6 above:

Please insert a disk into USB drive (X:)
error
Error message

This message can be ignored as shortly after, Windows completes the formatting process of the partition and creates the partition successfully.

How to Format Drive using Command Prompt

Diskpart is a command line utility used to manage Windows computer drives, including disks, partitions, volumes and virtual drive disks. It can be used to format a drive safely and securely. We will use diskpart utility to format the drives from Command Prompt.

You can also format a USB drive using Command Prompt. However, this tool requires 2 different methods for a situation, depending on whether there is already a drive letter assigned to a USB partition.

Format Drive with Drive Letter

Perform the following steps using the Command Prompt to format the drive if a drive letter is already assigned to it.

Note: This method will only format the existing partition(s) on the USB drive and will not resize them.

  1. Plug in the USB device to your computer.
  2. Open the Command Prompt with administrative privileges and paste the following to obtain the drive letter(s) of the USB partition(s).
    WMIC LogicalDisk Get DeviceID, VolumeName, Description
    cmd get
  3. Now that you have the associated drive letter, use the following cmdlet to format the partition, while replacing DriveLetter with the drive letter of the partition and Label with the new label that you want to give.
    Note: Press Enter twice to confirm the volume is attached to your computer.
    Format <em>DriveLetter</em>: /v:<em>Label </em>/fs:NTFS /q
    cmd format
  4. Repeat step 3 in case you wish to format more volumes on the drive.

That’s it! The drive partition will now be formatted successfully.

Format Drive Without Drive Letter

If your USB partitions do not yet have a drive letter, use this method to format them using the Command Prompt.

  1. Attach the USB to your computer.
  2. Open the Command Prompt with administrative privileges and paste the following to enter Disk Partition mode:
    Diskpart
    cmd diskpart
  3. Now paste the following and note down the disk number associated with the USB drive.
    List Disk
    cmd list disk
  4. Now select this disk using the given cmdlet, while replacing Number with the disk number noted in step 3.
    Select Disk <em>Number</em>
    cmd select disk
  5. Now delete all the data on the disk using this cmdlet:
    Clean
    cmd clean
  6. Now create a single, big partition on the drive using this cmdlet.
    Create partition Primary
    cmd create partition primary
  7. Now enter the following command to quickly format the partition. Replace Label with any custom label that you want to assign to the volume.
    Format fs=NTFS Label="<em>Label</em>" quick
    cmd format 2
  8. Now paste the following cmdlet to automatically assign a drive letter to this partition.
    Assign
    cmd assign

The USB drive will now be formatted successfully and will automatically open in Windows Explorer.

Closing Words

Although many of you would prefer to format a drive using the GUI-based Explorer, using the command-line has its advantages. If you are a sysadmin who spends most of their time on the command line, especially PowerShell, then you would know that it enables you to perform all sorts of administrative tasks from a single interface.

Similarly, you can now also format drives, according to your needs and specifications, directly from Command Prompt or Windows PowerShell, without having to switch between windows.

Also see:

Subscribe to our Newsletter

Get the latest tech news, advice and downloads in your inbox

Leave a Reply

You have to agree to the comment policy.