How to Clean Windows PC Using Command Prompt

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

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

Start cleaning your computer with CMD

Below is a list of commands to launch disk cleanup, clean temporary files, cache, etc. Some of these commands may require you to open Command Prompt with admin rights .

How to defragment hard drive using Command Prompt?

While fragmentation on traditional hard drives is normal, it can cause performance issues. It affects the read and write speeds of the drive, making your system run slowly.

How to Clean Windows PC Using Command Prompt
Windows Command Prompt during defragmentation, displaying information about the drive and expected results

Defragmentation rearranges fragmented data to help your drive run more efficiently. You don't need to defragment your SSD (it's not recommended). But if you have a mechanical hard drive, here's how to defragment it using the command line.

  1. Open Command Prompt with admin rights.
  2. In Command Prompt, type the following command and press Enter :
    defrag c:
  3. In the above command, c: is the drive you want to defragment. Change this letter if you want to defragment a different drive.

You can run the defrag command using optional switches. Refer to the article: Defrag Command in Windows to better understand how to use different syntax with the defrag command to perform analysis, add exceptions, change priorities, etc.

Clean up your drive quickly with Run

How to Clean Windows PC Using Command Prompt
Clean up disk using Run command

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

To run Disk Cleanup:

  1. Press Win + R to open Run .
  2. Enter the following command in the Run dialog box:
    C:\windows\SYSTEM32\cleanmgr.exe /dDrive
  3. In the above command, replace Drive with the drive letter you want to wipe. For example, if you want to perform a quick wipe on drive E , the full command would be as follows:
    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 the drive you specified selected.
  6. Select the files to delete and press OK .

How to use 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, the Recycle Bin, and even system files.

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

Delete basic file

How to Clean Windows PC Using Command Prompt
Windows Disk Cleanup program, a GUI application launched from Command Prompt

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

  1. Open Command Prompt, type cleanmgr and press 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 Delete Files to confirm the action.

Clean up a specific drive/partition

How to Clean Windows PC Using Command Prompt
Clean up a specific drive/partition

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

cleanmgr /D C

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

Configure Disk Cleanup tool settings

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

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

Here's how it works:

1. Type the following command into Command Prompt and press Enter :

cleanmgr /sageset:1

2. In the above command, replace 1 with any number from 1 to 9999 . This tutorial will use 1 . When executed, it will open the Disk Cleanup tool and display all the junk items that you can delete.

How to Clean Windows PC Using Command Prompt
All the junk items you can delete

3. Select or deselect all the items you want to delete. For example, select temporary setup files, Windows Update Cleanup, downloaded program files, Recycle bin (be careful with this one), etc.

How to Clean Windows PC Using Command Prompt
Select file types to delete

4. After selecting the file types to clean up, click OK to save the configuration.

How to Clean Windows PC Using Command Prompt
Press OK to save the configuration

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 the file types specified earlier and assigned a value of 1. In this case, it will delete temporary setup files, Windows Update Cleanup, downloaded program files, and Recycle bin files.

Optimized for low space

Switch lowdisk , as the name suggests, is useful if you are running out of storage space on your hard drive. When executed, it will automatically check all file types.

The lowdisk command is followed by the drive letter for the partition to free up space. The complete command would 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 user prompts, use the following command instead:

cleanmgr /verylowdisk /d

How to delete temporary files using Command Prompt

Windows creates temporary files for its own use. They rarely take up a lot of space on your hard drive and are essential for the system to run smoothly. When the task is complete, your system automatically removes them from the temporary folders.

The Disk Cleanup tool will clean up temporary files older than 7 days. But if you need to clean up your temporary folder regularly, you can do it manually or use the Command Prompt.

How to Clean Windows PC Using Command Prompt
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 File Explorer or use the following command to delete temporary files:

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

Command Prompt will automatically skip any files that are currently in use but it will delete the rest.

How to delete prefetch files using Command Prompt

Prefetch files are temporary files created when an application runs on your Windows system. These files contain information used to optimize program execution.

Like other temporary files, prefetch files don't usually take up much space on your hard drive. However, if you need to delete prefetch files regularly, you can use Command Prompt to clean them up on your system.

To delete prefetch files using Command Prompt:

1. Open Command Prompt with admin rights.

2. In the Command Prompt window, type the following command to view the prefetch files:

%SystemRoot%\explorer.exe C:\Windows\prefetch\

3. The command will open the Prefetch folder in File Explorer and display the prefetch files that you can delete.

How to Clean Windows PC Using Command Prompt
Prefetch folder

4. To delete prefetch files, use the following command and press Enter :

del C:\Windows\prefetch\*.*/s/q

5. Command Prompt will display a list of all the deleted prefetch files.

Wipe hard drive with Diskpart

If you want to wipe the entire disk, you can use the diskpart utility. Diskpart is a Windows command line utility that supports more than 38 commands for different functions.

To wipe a drive, you can use diskpart's clean command. Once executed, it will delete all data and convert the drive into unallocated space.

How to Clean Windows PC Using Command Prompt
Windows Command Prompt running the DiskPart program, with available drives listed

Be careful when using the diskpart utility. Using the wrong objects can result in total data loss and you may not be able to recover any data. Therefore, make a backup of your important system data before using the diskpart tool.

To erase a drive:

  1. Type diskpart in Command Prompt and press Enter.
  2. Next, type list disk to see all the drives installed on your system.
  3. Select the drive you want to wipe. For example:
    Select disk 0
  4. If the drive status shows offline, type online disk and press Enter .
  5. To wipe the drive, type the following command and press Enter :
    Clean all
  6. Once done, type exit to close diskpart.
Sign up and earn $1000 a day ⋙

Leave a Comment

How to use Command Prompt, how to use cmd on Windows

How to use Command Prompt, how to use cmd on Windows

Hidden deep within Windows is a world of command lines that few people know about. In this article, we will guide you on how to fix errors and make your computer more secure using the cmd command line tool.

Summary of common Run CMD commands

Summary of common Run CMD commands

Instead of performing manual and direct access operations on Windows, we can replace them with available CMD commands for faster access.

8 CMD Commands to Manage Wireless Networks on Windows

8 CMD Commands to Manage Wireless Networks on Windows

To manage wireless network connections on your Windows 10 computer, you can use the Settings app or Control Panel. However, there are many advanced settings that you cannot find in the Settings app or Control Panel. In this case, you can use Command Prompt.

How to map a network drive in Windows 10

How to map a network drive in Windows 10

Accessing files from your system's local hard drive is one of the easiest things you can do on your computer. But what if you want to access files located on other systems' hard drives?

Configure TCP/IP from the command line

Configure TCP/IP from the command line

To configure TCP/IP like IP address, Subnet Mask, Default Gateway, DNS and WINS addresses and many other options you can use Netsh.exe or Netsh command.

How to fix the error “Ethernet doesnt have a valid IP configuration”

How to fix the error “Ethernet doesnt have a valid IP configuration”

Sometimes DHCP cannot get a valid IP address from the Network Interface Card, it will show an error message “Ethernet doesn't have a valid IP configuration”.

12 useful Command Prompt tips on Windows 10

12 useful Command Prompt tips on Windows 10

Command Prompt can automate many tasks, handle problems and perform all kinds of functions. The following 12 Command Prompt tips will help you make the most of this utility on Windows 10.

How to add the Open command window here feature when right-clicking on every folder

How to add the Open command window here feature when right-clicking on every folder

The Command Prompt (CMD) window is no stranger to users who like to mess with their computers, this window can tweak some important things on the system or the folders and data are installed outside. by lines of code.

Fix Windows Error Code 0xc0000098: Backup ACPI.sys to Prevent Boot Failure

Fix Windows Error Code 0xc0000098: Backup ACPI.sys to Prevent Boot Failure

Error code 0xc0000098 in Windows causes a blue screen error on startup. The ACPI.sys driver is often to blame, especially on unsupported versions of Windows 11 like 23H2 or 22H2 or Windows 10.

Quickly fix Windows 10/8/7 computer error that freezes and cannot exit Safe Mode

Quickly fix Windows 10/8/7 computer error that freezes and cannot exit Safe Mode

In some cases, sometimes you start your computer in Safe Mode and the computer freezes. So how to fix the error in this case, please refer to the article below of WebTech360.

How to fix Snipping Tool keeps showing error

How to fix Snipping Tool keeps showing error

Snipping Tool is a powerful photo and video capture tool on Windows. However, this tool sometimes has some errors, such as constantly displaying on the screen.

How to get the fastest internet connection from your router

How to get the fastest internet connection from your router

Every network has a router to access the Internet, but not everyone knows how to make the router work better. In this article, we will introduce some router tips to help speed up the wireless connection in the network.

Common Mistakes When Performing Internet Speed ​​Tests

Common Mistakes When Performing Internet Speed ​​Tests

If the results seem slower than expected, don't be too quick to blame your ISP - mistakes when performing the test can skew your numbers.

What is QoS? How to use QoS for faster Internet speeds when you need it most

What is QoS? How to use QoS for faster Internet speeds when you need it most

In the article below, we will introduce and guide you to learn about the concept of QoS - Quality of Service on router devices.

Why cant this built-in Credential Manager feature in Windows replace a password manager?

Why cant this built-in Credential Manager feature in Windows replace a password manager?

Windows has a built-in Credential Manager, but it's not what you think it is—and certainly not a replacement for a password manager.

How to install free HEVC codecs on Windows 10 (for H.265 video)

How to install free HEVC codecs on Windows 10 (for H.265 video)

Microsoft charges for its official codecs and doesn't include them in Windows 10. However, you can get them for free without pulling out your credit card and spending $0.99.

Instructions to fix Access Denied error when accessing files or folders on Windows

Instructions to fix Access Denied error when accessing files or folders on Windows

While having trouble accessing files and folders can be frustrating, don't panic—with a few simple tweaks, you can regain access to your system.

How to check if USB Boot has been created successfully?

How to check if USB Boot has been created successfully?

After creating a USB boot to install Windows, users should check the USB boot's performance to see if it was created successfully or not.

4 Types of Windows Data That Are Growing in Size: How to Control Them?

4 Types of Windows Data That Are Growing in Size: How to Control Them?

Although Windows Storage Sense efficiently deletes temporary files, some types of data are permanent and continue to accumulate.

How to turn off background apps in Windows 11, reduce RAM consumption on Win 11

How to turn off background apps in Windows 11, reduce RAM consumption on Win 11

You can turn off Windows 11 background apps to optimize performance, save battery and reduce RAM consumption.

Instructions to fix the error “Network path was not found” in Windows

Instructions to fix the error “Network path was not found” in Windows

When a computer, mobile device, or printer tries to connect to a Microsoft Windows computer over a network, for example, the error message network path was not found — Error 0x80070035 may appear.

How to block Internet connection of Windows 10 software and applications

How to block Internet connection of Windows 10 software and applications

We can block Internet access for any application or software on the computer, while other programs can still access the network. The following article will guide readers on how to disable Internet access for software and applications on Windows.

How to Stay Safe on Windows 10 Until 2030

How to Stay Safe on Windows 10 Until 2030

Instead of paying $30 for a year of security updates, keep Windows 10 safe until 2030 with this simple solution.