How to Defragment Hard Drive using Batch file in Windows 11/10?

Windows 11 and Windows 10 come with a basic defragmentation tool. If your PC is equipped with an SSD, you don’t need to defrag it; however, you should take care of defragmenting at least once a week if you are using HDD. Windows default defragment tool is not so powerful and does not feature advanced options, such as it can not defrag multiple drives at once. If you are looking for an advanced and free defragmentation tool, you can opt for Puran Defrag, IObit Smart Defrag, DiskTuna, or UltraDefrag. However, these apps are old but work well with Windows 11/10.

Although you can not include all the features offered by third-party freeware tools by tweaking the registry or other methods, it is possible to make a batch file to run automatic drive optimizations regularly by default. If you would like to defrag a single hard drive or multiple hard drives, you can defrag your hard drive by making a batch file. Once you are ready with a batch file, just run it with elevated rights to defragment your HDD. Isn’t it easy?

How to Defragment Hard Drive using Batch File on Windows 11/10?

By default, Windows 11/10 runs automatic drive optimizations regularly. However, if your PC is busy, Windows may skip the defragmentation process resulting in slow performance. To get the best from your computer, you should defrag HDD regularly. Doing defragmentation manually on every drive is a time-consuming task. You have multiple drives to defrag; you might prefer to create a batch file to defrag it. Once the batch file is ready, just run them with administrative privileges. For that, you need to do the following:-

Step 1. Bring up the Run dialog box by pressing Windows + R key simultaneously from the keyboard.

Step 2. In the Run box, type the following and hit Enter from the keyboard to open the Notepad application:-


Step 3. Assume you have five drives with letters No C, D, E, F, and G. Copy and paste the following in the notepad:-

@echo off
defrag.exe C: -f
defrag.exe D: -f
defrag.exe E: -f
defrag.exe F: -f
defrag.exe G: -f

If on your PC, the drives letters are different than our example, you need to edit it according.

Step 4. Now, on the notepad file, click on the File menu and then select Save as option from the drop-down list.

Step 5. On the Save As dialog, select the file’s location where you would like to save this batch file. In the “File name” box, type defragment.bat or defragment.cmd. From the “Save as type” drop-down menu, select the “All Files” option. Once done, click on the Save button.

Step 6. To defragment all the available drives at once, right-click on the defragment.bat file that you have created using the above steps, and select the Run as administrator option.

That’s all. If you want to run this batch file automatically, it is also possible to set it on Windows 11/10.

How to schedule a Batch File to run automatically in Windows 11/10?

Follow these steps to schedule a Batch File to run automatically in Windows 11/10:-

Step 1. Press the Windows key from the keyboard to open the Start menu and then start typing Task Scheduler.

Step 2. In the available search results, click on the Task Scheduler to open it.

Step 3. On the Task Scheduler window, click on the Create Basic Task from the right side pane.

Step 4. Under Create Basic Task Wizard, type in the name you would like and then click the Next button.

Step 5. From the Trigger, select the option you like and then click on the Next button.

I chose Weekly and then clicked the Next button; you can select other options according to your needs.

Step 6. Select any weekday and time on which you want to execute that batch file, and then click on the Next button.

Step 7. Now, select Start a program and then click on the Next button.

Step 8. On the upcoming window, click on the Browse button and select the batch file from your PC that you would like to run. Then click on the Next button.

Step 9. Finally, click on the Finish button to create the task.

That’s all.

Using the same method, you can create scheduled tasks for any other batch file or an application on your Windows 11/10/8/7 PC.


In conclusion, defragmenting your hard drive is crucial for maintaining your PC’s performance, especially if you are using an HDD. While Windows 11/10 comes with a basic defragmentation tool, third-party defragmentation tools offer more advanced features. However, creating a batch file to defrag multiple drives is a convenient option. The process is straightforward, and once you have created the batch file, you can run it with administrative privileges to defrag all the available drives at once. Additionally, you can schedule the batch file to run automatically using the Task Scheduler feature in Windows 11/10. By following the steps outlined above, you can keep your PC running efficiently and maintain its overall health.

Also Read:

How to Delete a Scheduled Task in Windows 11/10?

Leave a Reply