管理磁盘的最简单、最直接的方法之一是使用 Windows 中的磁盘管理(Disk Management)工具。但是,嘿,磁盘管理(Disk Management)是一个可视化工具,我们中的一些人更喜欢基于命令的界面。这就是为什么我们认为看看如何从命令提示符(Command Prompt)或PowerShell使用diskpart、chkdsk、defrag和其他命令行工具来完成几个磁盘管理(disk management)操作是一个好主意。在本文中,我们将向您展示如何直接从命令提示符(Command Prompt)创建、格式化、删除、隐藏和取消隐藏、碎片整理和检查分区是否有错误。让我们开始吧:
在前进之前要考虑的重要方面
为了能够执行本文中介绍的任何操作,您需要具有管理权限并以管理员身份运行命令提示符(Command Prompt)或PowerShell。根据您要使用的内容,阅读以下文章之一:
- 在Windows中启动(Windows)命令提示符(Command Prompt)的10 种方法
- 在Windows中启动PowerShell的 9 种方法(包括以管理员身份)
同样重要的是要承认,如果您滥用本文中列出的命令,您可能会丢失数据。在继续之前请小心并备份您的数据。如果发生任何不好的事情,我们确实警告过您!🙂
1.如何使用diskpart从命令提示符或PowerShell创建分区(Command Prompt or PowerShell)
在命令提示符(Command Prompt)或PowerShell中,键入diskpart以访问名为Diskpart的 Windows 工具。
启动diskpart后,是时候确定要用于创建新分区的硬盘或固态驱动器了。(state drive)您可以使用select命令选择驱动器。要查看系统上的可用磁盘,请运行命令list disk。
要选择要使用的磁盘,请键入select disk ,然后键入要使用的硬盘编号。
要创建新分区,您需要使用create partition命令后跟分区类型和大小(partition type and size)。请注意,之前使用的list命令还会显示每个硬盘上的可用空间。您的分区大小必须等于或小于可用空间量。
例如,要创建一个大小为 10240 MB (10 GB) 的新主分区,您必须键入"create partition primary size=10240"。使用相同的命令并根据您的需要和硬盘上的可用空间更改大小。
2. 如何使用 diskpart 从命令提示符或 PowerShell为分区分配字母(Command Prompt or PowerShell)
您可以并且可能还想为新创建的卷分配一个字母。为分区分配字母使其在文件资源管理器(File Explorer)中可见,以便您可以轻松访问它。要将字母分配给分区,请使用assign命令。
例如,如果您想将R字母分配给刚刚创建的分区,您应该输入"assign letter = R"。
请记住,在分配盘符(drive letter)之前,您需要启动Diskpart并选择要为其分配盘符的磁盘。
3.如何使用diskpart从命令提示符或PowerShell(Command Prompt or PowerShell)中删除分区的字母(隐藏分区)
如果您的分区包含您不希望应用程序和人员看到的私人信息,您可能希望将其从文件资源管理器(File Explorer)中隐藏。为此,您可以删除其分区号,Windows(partition letter and Windows)将停止使其易于访问。要隐藏分区,请在命令提示符(Command Prompt)或PowerShell中运行(PowerShell)diskpart ,然后选择要隐藏的卷。然后,运行命令remove letter=R其中R是该分区的分配字母。就是这样:现在分区在文件资源管理器(File Explorer)中不再可见。
4. 如何在命令提示符和 PowerShell中使用 diskpart 格式化分区(Command Prompt and PowerShell)
尽管您现在在计算机或设备(computer or device)上创建了一个新分区,并且还分配了一个字母,因此很容易访问它,但您仍然无法使用它,除非您也格式化该分区。为此,请使用命令"format fs=ntfs quick"。使用帮助格式(help format)可了解有关此命令的更多信息。
此命令应在启动diskpart并选择要格式化的磁盘后使用。
5. 如何使用 diskpart 从命令提示符或 PowerShell中删除分区(Command Prompt or PowerShell)
当您需要从计算机中删除驱动器时,过程非常简单。首先(First),打开diskpart应用程序。然后,选择要删除的分区并键入删除分区(delete partition)命令。
您可以在下面看到删除操作如何工作的示例。只需选择找到分区的磁盘,然后选择要删除的卷并运行删除命令即可。
使用完diskpart后,键入exit命令关闭程序。
6. 如何使用 chkdsk 从命令提示符或 PowerShell中检查驱动器的错误(Command Prompt or PowerShell)
要检查磁盘是否有错误,您必须使用chkdsk命令。键入chkdsk,后跟驱动器号。我们建议您始终将此命令与/f参数结合使用。这使该工具能够自动修复它发现的任何错误。
例如,我们通过键入chkdsk /f r:并按Enter来扫描 R: 驱动器以查找错误。扫描过程立即开始。
扫描完成后,您会看到与下图类似的报告。
这总结了分区有多少磁盘空间(disk space),系统正在使用多少文件,磁盘上有多少可用空间,识别出多少坏扇区等等。
7. 如何使用碎片整理从命令提示符或 PowerShell优化驱动器(Command Prompt or PowerShell)
命令提示符(Command Prompt)和PowerShell还为您提供了一种优化(修剪或碎片整理)分区的方法。您所要做的就是键入命令defrag,后跟驱动器的盘符。例如,如果您想优化 R: 分区,您可以使用"defrag r:"。如果一切正常,在您选择的卷经过优化(如果是SSD则修剪,或者如果是硬盘则进行碎片整理)后,您应该会得到如下所示的报告。
该报告包括优化过程(optimization process)之前和完成之后的信息,例如卷大小(volume size)、可用空间、总碎片空间和最大可用空间大小(space size)。
您觉得本指南有用吗?
尽管您可能认为通过命令提示符(Command Prompt)或PowerShell管理分区很困难,但我们希望我们能够证明这并不困难。所涉及的过程一点也不复杂,一旦您学会了如何使用diskpart并知道正确的命令,使用命令提示符(Command Prompt)或PowerShell可以比使用其他工具(如磁盘管理(Disk Management))快得多。如果您有任何疑问,请随时在下面发表评论。
How to use diskpart, chkdsk, defrag and other disk-related commands
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 is why we thought it would be a great idea to see how several disk management actions can be done from the Command Prompt or PowerShell, using diskpart, chkdsk, defrag, and other command-line tools. In this article we show you how to create, format, delete, hide and unhide, defragment and check your partitions for errors, directly from the Command Prompt. Let's get started:
Important aspects to consider before moving forward
To be able to perform any actions presented in this article, you need to have administrative rights and run the Command Prompt or PowerShell as administrator. Read one of the following articles, depending on what you want to use:
It is also 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, we did warn you! 🙂
1. How to use diskpart to create a partition from the Command Prompt or PowerShell
In Command Prompt or PowerShell, type diskpart to access the Windows tool called Diskpart.
After you have launched diskpart it is time 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. To view the available disks on your system, run the command list disk.
To select the disk you want to work with, type select disk followed by the number of the hard disk you want to use.
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, to make a new primary partition with the size of 10240 MB (10 GB), you must type "create partition primary size=10240". Use the same command and change the size according to your needs and the available free space on your hard disk.
2. How to use diskpart to assign a letter to a partition from the Command Prompt or PowerShell
You can and 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 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".
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 use diskpart to remove the letter of a partition from the Command Prompt or PowerShell (hide a partition)
If you have a partition that contains private information that you do not want to be seen by apps and people, you might want to hide it from File Explorer. To do so, you can remove its partition letter and Windows stops making it easily accessible. To hide a partition, run diskpart in Command Prompt or PowerShell and select the volume that you want hidden. Then, run the command remove letter=R where R is that partition's assigned letter. That is all: now the partition is not visible in File Explorer anymore.
4. How to use diskpart to format a partition from the Command Prompt and PowerShell
Although 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, 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 about this command.
This command should be used after you start diskpart and select the disk that you want to format.
5. How to use diskpart to delete a partition from the Command Prompt or PowerShell
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.
When you are done working with diskpart, type the exit command to close the program.
6. How to use chkdsk to check a drive for errors from the Command Prompt or PowerShell
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 are scanning the R: drive for errors by typing chkdsk /f r: and pressing Enter. The scan process starts immediately.
After the scan is finished you see a report similar to the one from the picture below.
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.
7. How to use defrag to optimize a drive from the Command Prompt or PowerShell
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. If you wanted to optimize 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 optimized (trimmed if it is an SSD, or defragmented if it is a hard disk).
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.
Have you found this guide useful?
Even though you might think it is hard to manage your partitions through the Command Prompt or PowerShell, we hope that we managed to prove that it is not that difficult. The procedures involved are not complicated at all and, once you learn how to use diskpart and know the correct commands, using the Command Prompt or PowerShell can be a lot faster than using other tools like Disk Management. If you have any questions, feel free to leave a comment below.