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 do not need to defragment it; However, you should defragment at least once a week if you are using HDD. The standard Windows defragmentation tool is not as powerful and does not offer advanced options such as: If you are looking for an advanced and free defragmentation tool, you can go for Puran Defrag, IObit Smart Defrag, DiskTuna or UltraDefrag. However, these apps are old but work fine with Windows 11/10.

While you cannot capture all of the functionality of third-party freeware tools by optimizing the registry or other methods, it is possible to create a batch file by default to perform automatic drive optimizations on a regular basis. If you want to defragment a single hard drive or multiple hard drives, you can defrag your hard drive by creating a batch file. Once you are done with a batch file, simply run it with elevated rights to defragment your hard drive. Ain’t it easy

Topics covered

How do you defragment the hard drive with the batch file in Windows 11/10?

By default, Windows 11/10 performs automatic drive optimizations on a regular basis. However, if your PC is busy, Windows can skip the defragmentation process, resulting in slow performance. To get the most out of your computer, you should defragment your hard drive regularly. Manual defragmentation on each drive is a time consuming task. You have multiple drives to defragment; You may prefer to create a batch file to defragment it. Once the batch file is ready, just run it with administrator rights. To do this, you need to do the following: –

Step 1. Call the. on Run Dialog box by pressing window + R. press simultaneously from the keyboard.

Step 2. Im Run Enter the following and press Enter from the keyboard to open the notepad Application:-


Step 3. Assuming you have five drives labeled # C, D, E, F, and G. Copy and paste the following into 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 your PC drives have different letters than those shown in our example, you’ll need to edit them accordingly.

Step 4. Now click on the in the Notepad file file Menu, then select Save as Option from the drop-down list.

Step 5. In the Save As dialog box, choose the location of the file where you want to save this batch file. By doing “Filename“Field, type, defragment.bat or defragment.cmd. Of the “Save as”Drop down menu, select the“All files“Option. When you’re done, click that to save Button.

Step 6. To defragment all available drives at once, right-click the defragment.bat file you created using the steps above and select Execute as administrator Possibility.

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

How do I schedule a batch file to run automatically in Windows 11/10?

Step 1. Press the window Key from the keyboard to open the Start menu and then start typing Task planner.

Step 2. Click the from the available search results Task planner to open it.

Step 3. On the Task planner Window, click that Create a simple task from the right side area.

Step 4. Under Wizard for creating simple tasks, enter the name you want, and then click Next Button.

Step 5. From the Trigger, select the option you want, and then click Next Button.

I chose Weekly and then clicked Next Button; You can choose other options as needed.

Step 6. Select a day of the week and a time that you want to run this batch file, then click Next Button.

Step 7. Now choose Start a program and then click on Next Button.

Step 8. In the next window, click the Search and select the batch file from your PC that you want to run. Then click on that Next Button.

Step 9. Finally, click the Finished Button to create the task.

That’s all.

You can use the same method to create scheduled tasks for any other batch file or application on your Windows 11/10/8/7 PC.

also read:

How do I delete a scheduled task in Windows 11/10?