Key Takeaways

  • Command Prompt is a useful tool for cleaning up your Windows computer, offering efficiency and performance benefits.
  • Command Prompt can defragment your hard disk, improving read and write speed and overall system performance.
  • The Disk Cleanup tool can be accessed and customized using Command Prompt, allowing you to delete specific types of files.

Keeping your computer free from clutter can help improve performance and free up tons of storage space. Windows has several built-in system cleanup utilities, including the Disk Cleanup tool. However, the Command Prompt is a lesser-known way to clean your computer.

Here are a few ways to clean up a slow Windows computer using Command Prompt.

Why Clean Up Your Computer Using the Command Prompt?

Command Prompt is a built-in command processor available on almost all Windows versions since Windows 3.1. While the graphical user interface is handy for most people, some may prefer the Command Prompt to perform several tasks for its efficiency and performance benefits.

If you are new to Microsoft’s popular command processor, check out our beginner’s guide to the Windows Command Prompt.

The Disk Cleanup tool and the Storage Sense feature in Windows 11 make it fairly easy to clean up your computer. If any unwanted files remain, you can remove them manually by, for example, clearing your temp folder. You can speed up this process with the help of commands to access the Disk Defragmenter, diskpart, and the Disk Cleanup tool.

Start Cleaning Your Computer Using CMD

Below is the list of commands to launch the disk cleanup utility, clean temporary files, memory cache, and more.

Some of these commands may require you to open Command Prompt as administrator. Here’s how to do it.

  1. Type cmd in the Windows search bar.
  2. Right-click on Command Prompt and select Run as administrator.

How to Defragment Hard Disk With the Command Prompt?

While disk fragmentation on traditional hard disks is natural, it can cause performance issues. It affects your disk's read and write speed, making your system slow.

The Windows Command Prompt in the process of defragmentation, showing volume information and the expected results

Defragmentation rearranges fragmented data to help your disks work more efficiently. You don’t need to defragment your SSD storage devices (nor is it recommended). But if you use a mechanical hard drive, here’s how to defragment the hard disk with a command.

  1. Open the Command Prompt as administrator.
  2. In the Command Prompt, type the following command and hit enter:
            defrag c:
        
  3. In the above command, c: is the drive you want to defrag. Change the drive letter if you want to defrag another drive.

You can run the defrag command with optional switches. Check Microsoft's documentation on using the defrag command for more insight into using different syntax with the defrag command to perform analysis, add exceptions, change priority, and more.

Quick Disk Cleanup Using the Run Box

disk cleanup using run command

You can use a Run command to perform a quick disk cleanup without opening the Command Prompt. It is useful if you want to free up a few gigabytes of storage in two steps. This requires you to start the Disk Cleanup tool and specify the disk you want to clean via the Run dialog.

To run Disk Cleanup:

  1. Press Win + R to open Run.
  2. Type the following command in the Run dialog:
            C:\windows\SYSTEM32\cleanmgr.exe /dDrive
        
  3. In the above command, replace Drive with the disk drive letter you want to clean. For example, if you want to perform a quick cleanup for the E drive, then the full command will look like this:
            C:\windows\SYSTEM32\cleanmgr.exe /dE
        
  4. Click OK or press Enter to execute the command.
  5. Run will quickly launch the Disk Cleanup tool with your specified drive selected.
  6. Select the files to delete and click OK.

How to Use the Disk Cleanup Utility via Command Prompt

Disk Cleanup is a built-in Windows utility to help you free up space on your computer’s hard drive. It can clean up downloads, temporary internet files, recycle bin, and even system files.

You can use the Command Prompt to launch Disk Cleanup and perform automated cleanup tasks directly. Here is how to use the tool and supported command-line switches.

Basic File Deletion

The Windows Disk Cleanup program, a GUI app launched from a command prompt

You can use the cleanmgr command to launch the Disk Cleanup tool using Command Prompt.

  1. Open Command Prompt, type cleanmgr, and hit Enter.
  2. In the Drive Selection window, select the drive you want to clean up and click OK.
  3. In the Disk Cleanup window, select all the files you want to delete and click OK.
  4. Click on Delete Files to confirm the action.

Clean Up a Specific Drive/Partition

cleanmgr specific drive

You can use the cleanmgr command followed by a drive letter to run the Disk Cleanup tool against that drive. This is useful to free up space on a specific drive. For example, run the following command to run the Disk Cleanup tool against the C drive:

        cleanmgr /D C
    

The Disk Cleanup tool will scan and show all the files you can delete. Make your selection and click OK.

Configure the Disk Cleanup Tool Settings

To customize the Disk Cleanup tool settings, the cleanmgr command supports multiple switches, including sageset and sagerun.

  • sageset:x allows you to configure the Disk Cleanup tool’s settings. When executed, it will launch the Disk Cleanup tool and let you select specific items you want to delete and assign them to the value x. The x value, which can be any value from 1 to 9999, is stored in the registry.
  • sagerun:x runs the specified task assigned to the x value and cleans all the specified files automatically.

Here’s how it works:

  1. Type the following command in Command Prompt and press enter:
            cleanmgr /sageset:1
        
  2. In the above command, replace 1 with any number between 1 and 9999. For this guide, we’ll go with 1. When executed, it will open the Disk Cleanup tool and show all the junk items you can delete.
    cmd cleanmgr sageset 1
  3. Select or unselect all the items that you want to delete. For example, select Temporary setup files, Windows Update Cleanup, Downloaded program files, Recycle bin (be careful with this one), etc.
    disk cleanup select file types
  4. After selecting the types of files to clean up, click OK to save the configuration.
    cleanmgr sagerun 1
  5. Now, use the following command to run the Disk Cleanup tool with your custom configuration:
            cleanmgr /sagerun:1  
        
  6. This will launch the Disk Cleanup tool and start cleaning up the file types specified previously and assigned to the value 1. In this instance, it will delete Temporary setup files, Windows Update Cleanup, Downloaded program files, and Recycle bin files.

Optimize for Low Space

The lowdisk switch, as the name suggests, is useful if you are running low on storage space on your hard disk. When executed, it automatically checks all file categories.

The lowdisk command is followed by the drive letter for the partition to free up space. A complete command will look something like this:

        cleanmgr /lowdisk /d
    

When executed, Disk Cleanup will open with all the junk file categories selected in the D: drive.

To delete all junk files quickly, without the user prompt, use the following command instead:

        cleanmgr /verylowdisk /d
    

How to Clear Temporary Files Using the Command Prompt?

Windows creates temporary files for momentary use. They rarely occupy a large space on your hard drive and are essential for the smooth functioning of your system. When the task is complete, your system should automatically discard them from the temporary folders.

The Disk Cleanup tool cleans temporary files that are older than seven days. But if you have to clean the temp folder frequently, you can do it manually or using the Command Prompt.

The Windows Command Prompt with the del command in progress, showing some individual files that cannot be deleted

To view temporary files, enter the following command in Command Prompt:

        %SystemRoot%\explorer.exe %temp%\
    

You can delete these files manually (Ctrl + A > Delete) from the File Explorer or use the following command to delete temp files:

        del %temp%\*.* /s /q
    

Command Prompt will automatically skip any currently used file, but it will delete the rest.

How to Delete Prefetch Files Using the Command Prompt

Prefetch files are temporary files created when an application is run on your Windows system. These files contain information that is used to optimize the execution of programs.

Like other temporary files, the prefetch files often don’t take up much space on your hard drive. However, if you need to delete the prefetch files frequently, you can use the Command Prompt to clean up these files on your system.

To delete the prefetch files using Command Prompt:

  1. Open Command Prompt as administrator.
    prefetch file folder windows
  2. In the Command Prompt window, type the following command to view the prefetch files:
            %SystemRoot%\explorer.exe C:\Windows\prefetch\
        
  3. It will open the prefetch folder in File Explorer and show the prefetch files that you can delete.
    prefetch file folder windows
  4. To delete the prefetch files, use the following command and hit enter:
            del C:\Windows\prefetch\*.*/s/q
        
  5. Command Prompt will show a list of all the deleted prefetch files.

Cleaning a Hard Disk Using Diskpart

If you want to wipe clean an entire disk, you can use the diskpart utility. Diskpart is a Windows command-line utility supporting over 38 commands for different functions.

To wipe clean a disk, you can use diskpart’s clean command. Upon execution, it deletes all the data and converts the disk into unallocated space.

The Windows command prompt running the DiskPart program, with available volumes listed

Exercise caution when using the diskpart utility. Using incorrect objects can cost you all of your data, and you may not be able to recover any of it at all. Therefore, create a backup of your critical system data before using the diskpart tool.

To clean a disk:

  1. Type diskpart In the Command Prompt and hit enter.
  2. Next, type list disk to view all the installed disks on your system
  3. Select the disk you want to wipe clean. For example:
            Select disk 0
        
  4. If the disk status shows offline, type online disk and hit enter.
  5. To wipe clean your disk, type the following command and hit enter:
            Clean all
        
  6. Once completed, type exit to close diskpart.

Keep Your Computer Clean From Junk Files Using Command Prompt

You can use the Command Prompt to perform various advanced actions, including removing junk files from your computer. While you can use the GUI-based Disk Cleanup tool, Command Prompt makes it easy to clean up specific types of files and wipe clean disks.