In addition to the Windows GUI, you can also use Command Prompt's quick utilities to clean your computer of junk.
Keeping your computer free of junk and clutter can help improve its performance and free up gigabytes of storage space.
Windows comes with several built-in utilities for cleaning up your system, such as the Disk Cleanup tool. However, a lesser-known way to clean your computer is through the Command Prompt.
Here are some ways to clean up a loaded and slow Windows computer using the command line.
Why clean your computer using the command line?
The Command Prompt is a built-in command editor available in almost all versions of Windows, from Windows 3.1 onwards.
While the GUI is easy to use for most people, some may prefer the Command Line for its efficiency, speed, and performance benefits.
If unwanted files remain, you can also remove them manually, for example by clearing the temp folder. You can speed up this process with the help of commands to access Disk Defragmenter, diskpart and Disk Cleanup tool.
Commands to clean your computer using CMD
Below is the list of commands to start disk cleanup utility, clean temporary files, cache and more.
Some of these commands may require you to open the command prompt as an administrator. For this do the following:
- Type cmd in the Windows search bar.
- Right-click on Command Prompt (or cmd.exe).
- Select “Run with administrator rights”.
How to defragment hard drive with command line
Fragmentation of a traditional mechanical hard drive is a natural phenomenon and can cause performance problems. It affects your disk access and write speed, making your system slow.
Defragmenting rearranges fragmented data to help your disks work more efficiently. You don't need to defrag your SSD storage devices (nor is it recommended), but if you're using a mechanical drive, here's how to defrag your hard drive with one command.
- Open Command Prompt as administrator.
At the command line, type the following command and press enter:
In the command above, c: is the drive you want to defragment. Change the drive letter if you want to defragment another drive.
You can run the defrag command with optional switches. This Microsoft Directive offers more information about using different syntax with the defrag command to run analysis, add exceptions, change priority, and more.
Quick disk cleanup using the Run window
You can use a command in Run to do a quick disk cleanup without even opening the command prompt.
- Press the Win + R keys simultaneously to open the Run window.
Type the following command in the Run dialog box:
In the command above, replace “Drive” with the drive letter you want to clean. For example, if you want to perform a quick cleanup for drive E, then the full command would look like this:
- Click OK or press Enter to run the command.
Run will quickly launch Disk Cleanup with the drive you've specified selected.
- Select the files you want to delete and click OK.
How to use the Disk Cleanup utility via the command line
Disk Cleanup is a built-in Windows utility that helps you free up space on your computer's hard drive. It can clean downloads, temporary internet files, recycle bin and even system files.
You can use the Command Prompt to start Disk Cleanup and perform some automated cleanup tasks directly. See how to use the tool and its supported switches.
Basic file deletion
You can use the cleanmgr command to launch Disk Cleanup using the command line. To use it:
- Open the command prompt, type cleanmgr and press Enter.
- In the Select Drive window, select the drive you want to clean and click OK. If you only have one disk this window will not open and you will go straight to #3.
3. Then, in the Disk Cleanup window, select all the files you want to delete and click OK.
4. Finally, click Delete files to confirm the action.
Skip drive selection
By running this command and putting where “disk” is the drive letter you want, it skips the drive selection step and directly displays the Disk Cleanup settings window. From here, you can select the files you want to delete.
Automatic file deletion
If you prefer to let the Disk Cleanup utility decide which files to delete, use the cleanmgr /sagerun variant. After running, it will scan your drives and delete unwanted files from your system.
Optimize for little space
The lowdisk switch, as the name suggests, is useful if you are running low on hard disk space. When run, it automatically scans all file categories by default.
When using this form of the command, enter the drive letter to free up space. The command will look something like this:
cleanmgr /lowdisk /d
When run, Disk Cleanup will open with all junk file categories selected from the D: drive.
To quickly delete all junk files without prompting the user, use the following command:
cleanmgr /verylowdisk /d
How to delete temporary files using the command line
Windows creates temporary files. Temp files rarely take up much space on your hard drive and are essential for your system to run smoothly. When the job is done, your system should automatically discard them from the temporary folders.
Disk Cleanup cleans temporary files that are older than seven days. But if you need to clean the temp folder frequently, you can do it manually or using the command line.
To view the temporary files, type the following command at the command prompt:
You can delete these files manually (Ctrl + A > Delete) from File Explorer or use the following command to delete temporary files:
del %temp%\*.* /s /q
The command line will automatically skip any file currently in use, but delete the rest.
How to delete Prefetch files using the command line
Prefetch files are temporary files that are created when an application is running on your Windows system. These files contain information used to optimize the performance of the programs.
Similar to other temporary files, Prefetch files often don't take up much space on your hard drive. However, if you want to delete them, you can use the Command Prompt for that.
To delete Prefetch files using the command line:
- Open the command prompt as an administrator.
- In the command prompt window, type the following command to view the prefetch files:
This will open the Prefetch folder in File Explorer and display the Prefetch files that you can delete.
3.To delete the prefetch files, use the following command and press enter:
del C:\Windows\prefetch\*.* /S /Q
The command prompt will display a list of all deleted prefetch files.
The /P and /Q switches are:
/P Asks for confirmation before deleting each file.
/Q Quiet mode, doesn't ask if it's OK, to delete with a global wildcard
Hard Disk Cleanup using Diskpart
If you want to clean an entire disk, you can use the diskpart utility. Diskpart is a Windows command line utility with support for more than 38 sub-commands for different functions.
To clean a disk, you can use the diskpart cleanup command. On execution, it erases all data and turns the disk into unallocated space.
Be careful when using the diskpart utility. Using wrong manipulations can cost you all your data and you may not be able to recover any of it anymore. Therefore, we suggest you to create a backup of your important system data before using diskpart tool.
To clean a disk:
1. Type diskpart in the command prompt and press enter.
2. Then type list disk to see all installed disks on your system
3.Select the disk you want to clean. For example:
Select disk 0 (or other equivalent disc number. Be careful which disc you choose!)
4. If the disk status shows offline, type online disk and press enter.
5. To clean your disk, type the following command and press enter:
6. Once complete, type exit to close diskpart.
Keep your computer clean of junk files using the command line
You can use the command line to perform various advanced actions, including removing junk files from your computer.
Although you can use the GUI-based Disk Cleanup tool for the same tasks, the command line makes it easier to clean single category files and wipe disks with its speed.