Defragging the disk in Windows PCs used to be an essential maintenance task, but is less important than it once was. However, there are still occasions when it can boost PC performance a little.
Read Do you still need a disk defragmenter for Windows PCs in 2017? if you want an introduction to defragmenting disks in Windows. Here we will look at how to do this from the command line, which offers more features, but needs a bit more technical knowledge than running it from the Windows interface.
Should you defrag the disk?
The answer isn’t yes or now, but maybe. If your computer has either an SSD (solid state disk) or a hybrid drive which is part SSD and part hard disk, then it should not be defragmented.
Files become fragmented over time and are literally broken into fragments. A file may be split into a hundred or more parts, each located in a different part of the disk.
With a mechanical disk that has spinning discs inside and arms that move read/write heads in and out, it takes a significant amount of time to locate all the parts of a fragmented file.
An SSD is simply reading memory locations and it doesn’t matter which part of memory is read, it is all read at the same speed. A file split into 100 fragments on an SSD can be read as fast as if it was one fragment.
Windows automatically defragments the disk every week in the background when the computer is idle. However, there are situations where it may not do it, which can lead to quite a lot of fragmentation.
The disk can become quite fragmented if you have a laptop and run it on the battery a lot. The reason is that defragging the disk takes a lot of power, so the task is skipped when a laptop is on battery power.
If you charge up the laptop, but don’t power it up and run it on mains power, defragging is put off and put off and fragments are growing in number.
Here is a laptop that has been running almost exclusively on battery power for a couple of weeks and the fragmentation is up to 46%. That is a lot and usually disks are worth defragmenting when they get over 10%.
You could plug in and power up your laptop and let it idle for an hour or two, or you could manually run the Windows defrag tool and force it to optimise the disk.
Defrag from the command line
Although there is a graphical tool to defragment the disk, there are more features in the command line version. Right click the Start button in Windows 10 and select Command Prompt (Admin). In Windows 7 go to Start, All Programs, Accessories, right click Command Prompt and run as an administrator.
To get a report showing the amount of fragmentation on the disk, enter
defrag c: /a
The c: is the drive letter and the /a means check for fragmentation and display a report.
If you want to see a constantly updated status when defrag is running, use /u. To get a verbose report with a lot more information about the status of the disk, use /v. Command line parameters can be used on their own or combined together, like this:
defrag c: /a /u /v
Normally the defrag tool runs at a low priority. This means that it runs slowly so as not to slow down any programs you might be using. From the command like you can force defrag to run with normal priority for programs and this will enable it to complete the job in less time. Enter:
defrag c: /h
Not only do files become fragmented, so does the free space on the disk. This may slow down writes to the disk as small blocks of free space are filled with a file as it is saved, causing fragmentation. It can also be a problem if you want to shrink a drive partition. Use the following command to consolidate all the free space:
defrag c: /x
It is possible to simultaneously defrag two disk drives, such as drive C: and drive D:, by adding the /m command line switch like this:
defrag c: d: /m
If you want to see a constantly updated status for defrag, use /u. These parameters can be used on their own or combined together, like this:
defrag c: d: /m /h /u
That command defrags drives C: and D: at the same time (/m) at normal priority (/h) and shows the progress (/u).
There are even more command line switches and they can be seen by entering defrag /? but they are mainly for specialist uses.