Windows

How to Clean Computer Using CMD

If your Windows PC is running out of storage space, you should remove unnecessary files and free up space as soon as possible. Cleaning up your computer can enhance its performance, and Windows already has tools like Disk Cleanup to do so. But, you can also use Command Prompt, which is a less popular but effective method to clean your PC.

This post will focus on how to clean computer using cmd with administrative permissions. An elevated command prompt refers to a Command Prompt window that has administrative privileges, which means it can perform actions that require higher permissions for modifying system files, installing software, or changing system settings, etc.

To open an elevated Command Prompt on Windows, you can search for CMD on windows search bar and then right-click on the Command Prompt icon and select “Run as administrator” from the context menu. This will bring you the CMD console with the Administrator tag on it.

clean-computer-using-cmd

Why Clean Up Your Computer Using Command Prompt?

Typically using the Windows cleanup recommendations, storage sense or the Disk clean up tool is effortless and helps you by suggesting removing temporary files that has no business on your PC and are safe to delete.

Temporary file remover

However, many people may prefer using the Command Prompt due to its efficiency and performance benefits when performing various tasks. If there are any unwanted files remaining on your computer, you can remove them manually by clearing the temp folder. This process can be speed up with the use of specific commands that allow access to useful tools such as Disk Defragmenter, diskpart, and Disk Cleanup.

Commands to clean computer using Command Prompt

There are multiple commands available to clean your PC via the Command Prompt. You can use commands to delete remaining unwanted files, clear temporary files, and even defragment your hard disk, all from the command line.

1. Clean unwanted files using Cleanmgr command class

This class of commands allows you to clean unwanted files and free up disk space on a computer’s hard drive. It will first analyze the files on your computer that are no longer of any use, then remove those files. It has sub-commands dedicated to specific functions.

1.1 Cleanmgr

Cleanmgr is a disk cleanup command line to clean junk. Follow these steps to use this command.

  • Cleanmgr is a disk cleanup command line to clean junk.
  • To use it, open the command prompt and type “Cleanmgr.”
  • A small window will appear where you have to select the drive to clean. 
  • Click on OK to ask your system to analyze the drive and find out removable junks.
  • Check which files you want to delete and press the OK button to execute.
how-to-clean-computer-using-cmd
Tutorial: clean computer using cmd – cleanmgr command

If you want to automate the disk cleanup task, then there are three command-line switches that Cleanmgr accepts. They are /sageset, /sagerun and /lowdisk.

1.2 Cleanmgr/sageset

This command will skip the disk selection step and take you straight to the category selection menu, where you need to check which category of junk files you want to clean.

The format of running the sageset command is;

cleanmgr/ sageset: numeric_id  

for example:

c:\Windows\system32> cleanmgr/ sageset: 1

1.3 Cleanmgr/ sagerun

This command will automate the disk-cleaning process without selecting the categories you need to use the sagerun command. 

The format of running the sagerun command is;

cleanmgr/ sagerun: id

for example:

c:\Windows\system32> cleanmgr/ sagerun: 1

1.4 Cleanmgr/lowdisk

This command is used when the disk drive is running in low disk space. When this switch is run, the disk cleanup opens checked with all checkboxes by default. The format of running the lowdisk command is;

cleanmgr/lowdisk/d{driver-latter of your disk}

for example:

c:\Windows\system32> cleanmgr/lowdisk/dc {for c drive}
c:\Windows\system32> cleanmgr/lowdisk/dd {for d drive}

2. Clean temporary files using cleanup command

Temporary files are referred to as Temp. These files store info for a transient period during the execution of an application, as well as they can be created for backup purposes. Primarily they are used to store, move data and recover lost data. 

It is safe to delete temporary files if and only if they are not being used by any application. Usually, Windows will prevent you from deleting these files during run time can result in application task failure.

The temporary files(Temp) created by OS are stored in %system%/windows/temp while temporary files (%Temp%) created by the users are stored in C:\Users\username\AppData\Local\Temp.

To view temp files using command prompt use: 

%SystemRoot%\explorer.exe %temp%\

And the command to delete temp files (you can manually delete them too from the directory):

del %temp%\*.*/s/q
delete-temp-files
Tutorial: clean computer using cmd – delete temp files

3. Clean prefetch files using command prompt

Windows creates all sorts of temporary files on your hard drive. Prefetch files are created by the operating system when an application is used for the first time and later works as a cache. They may cover lots of space based on how the application tends to use the dedicated file.

Prefetch is basically to make the app faster, but if you are facing app bugs or lags, you can delete these files.

To check the prefetch files on your computer simply check in the prefetch folder, like this (on cmd):

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

And then delete the files from the prefetch folder;

del C:\Windows\prefetch\*.*/s/q
delete-prefetch-using-cmd
Tutorial: clean computer using cmd – clean prefetch

Similar content: Tutorial to Remove Virus Using CMD in 4 Steps

4. Clear cache memory in windows

Cache files are helpful for application performance, but, If there’s something wrong with cache files, it can cause data fetching issues, glitches, or even crash. That’s why a common remedy is to clear application cache memory from time to time.

You can delete the DNS cache, Windows store cache from your PC without any hesitation. If there’s an issue with deleting these cache files, windows will notify you. To flush the DNS cache, start command prompt as administrator. 

  1. Search for the command prompt on the windows search bar.
  2. Right-click on the command prompt and select Run as administrator.
  3. Next in the command prompt run this command –
ipconfig/flushDNS

You will receive a message that lets you know the DNS cache is successfully flushed.

And to clear windows store cache memory, you will need to use the WSReset command in the cmd. Doing so will display a black screen like the image below, and all you need to do is wait until the windows disappear to be sure that windows store memory is successfully flushed.

windows-store-reset-command
clean computer using cmd – clean windows store cache

5. Defragment your hard disk with command prompt

Another part of cleaning computer memory is defragmenting your disks.

Defragmenting is the process of reorganizing the data stored in a hard drive. It is a process that reduces the degree of fragmentation by physically organizing the content of mass storage into the smallest number of adjacent regions. Reordering the files will allow your PC to access files faster from the hard disk.

Disk fragmentation is a natural occurrence on traditional hard disks, although it is vulnerable to PC performance. Basically, fragments affect the writing and accessing speed of your HDD, resulting in sluggish system performance.

To defragment your hard drive using cmd;

  1. Start the command prompt as administrator.
  2. Use defrag {preferred drive’s drive later}: defrag c: 

Note that c: in the defrag c: command is the drive letter that means the drive you want to defrag. Change the drive letter if you want to defrag another drive.

Defragment-Drive-C
clean computer using cmd – defragment your disks

7. Use Diskpart command to erase data from HDD

Diskpart is a command-line disk partitioning utility that is available in Windows 2000 and later editions. Previously Fdisk command was used for this task. You can use the diskpart command line to clean your disk along with specific switches like “clean” & “clean all” to erase data from the hard drive. 

To clean hard drives with the diskpart command follow the steps below;

  1. Start the command prompt as administrator.
  2. Type diskpart command and press enter. 
  3. Following the diskpart, type “list disk” in cmd.
  4. It will display all connected disks to your computer along with their availability status.
  5. The type select disk n {for example: select disk 0} 
  6. ** if the disk status is offline, type “online disk” and press enter.
  7. Then type “clean” or “clean all” according to your need.
  8. On success, you will see the “diskpart succeeded in cleaning the disk” message.

Clean command will only clean data on HDD and will be marked as deleted. However, the clean all command will delete every byte of data from the disk, and later you won’t be able to recover that data.

clean-diskpart
clean computer using cmd – diskpart command

????????  Related article: Best Ryzen 5 5600H Laptops Under 85k INR (November 2021)

8. DISM Windows image cleanup

If you are having trouble with the current operating system, then you can repair the issue with OS. It’s an image cleaning task that will restore the operating system’s health to its ideal state.

The command line you will use here is known as DISM (deployment image servicing and management), which is a built-in command line used to manage windows image (.wim) and virtual hard disk (.vhd). It is also used in the windows recovery environment and windows setup process, as well as in updating the operating system.

To check if there is any corruption, follow the steps below; Start cmd with admin privilege.

Start cmd with admin privilege > Type this command.

DISM /online /Cleanup-Image /checkhealth
dism-checkhealth
clean your computer using cmd – check OS health

And to scan the windows image use this command;

DISM /Online /Cleanup-Image /ScanHealth
dism-scanhealth
Clear your computer using cmd – scan health command

To fix the windows images, If you find any corrupted files use this command;

DISM /Online /Cleanup-Image /RestoreHealth /Source:repairSource\install.wim

You can scan your PC for malware/viruses using the command prompt. The scanning process will be performed by Windows defender.

Wrapping up: A command prompt is a powerful tool; you can it for many other purposes on your PC. If you have more info regarding PC clean commands, let us know in the comment below.

Similar articles:

  1. How to Install Kodi on Nvidia Shield and Nvidia Shield Pro
  2. Noise Cancellation Software You Must Try
  3. [Solved] Nvidia Graphics Card Not Detected on Windows
  4. How to Fix Razer Synapse Not Opening On Windows 10 & 11

contributor
I am Rupam, a Computer Application graduate with a passion for gadgets and technology. GameSpec is a platform I created to share my knowledge on gadgets and software tutorials. On this site, you can find helpful troubleshooting guides to assist you in solving common problems with software and gadgets.
Subscribe
Notify of
guest
2 Comments
Inline Feedbacks
View all comments
Tudor
Tudor
2 years ago

The defragmentation part isn’t harmful for a PC that uses SSD?

2
0
Want to share your thoughts?x
X