What is the command to run Disk Cleanup in powershell?

Disk Cleanup is a utility included in Windows that helps free up disk space by removing unnecessary and temporary files (https://disk-cleanup.com/). The main purpose of Disk Cleanup is to reclaim valuable hard drive space by identifying and safely deleting files that are no longer needed, including temporary internet files, temporary files, Recycle Bin files, and various system files (https://www.lenovo.com/ca/en/glossary/utility-software/). Running Disk Cleanup regularly can help optimize your PC’s performance by getting rid of clutter and ensuring there is adequate free space available.


In order to run Disk Cleanup in PowerShell, your system must meet the following prerequisites:

You must be running Windows 10 or Windows Server 2016 or later. Disk Cleanup is built into these versions of Windows and can be accessed through PowerShell. Older versions of Windows do not have native PowerShell support for Disk Cleanup.

You must have administrator privileges on the computer in order to run Disk Cleanup commands. Disk Cleanup deletes files and requires elevated permissions. Make sure to open PowerShell as an administrator before running Disk Cleanup cmds.

According to Microsoft’s documentation, Disk Cleanup “is designed to free up as much disk space as possible” by deleting temporary and other unneeded files. Having enough free disk space is important for performance. So Disk Cleanup can be an important tool, if your system meets the prerequisites.

Using the Cleanmgr Utility

The easiest way to run Disk Cleanup is through the Cleanmgr utility. Cleanmgr provides a simple graphical user interface to delete unnecessary files from your hard drive.

To use Cleanmgr:

  1. Open the Command Prompt as an administrator. This can be done by searching for “Command Prompt” in the Windows search bar, right-clicking on the Command Prompt app, and selecting “Run as administrator”.
  2. In the Command Prompt, type ‘cleanmgr’ and hit enter. This will launch the Disk Cleanup utility.
  3. Disk Cleanup will scan your drives and present a list of files that can be safely deleted. Check the box next to each drive you want to clean up.
  4. Click “OK” and Disk Cleanup will securely delete the selected files to free up space.

The Cleanmgr utility provides an easy way to launch the Disk Cleanup tool through a graphical interface. It allows you to selectively choose which drives to target for cleaning unnecessary files (Source). This method gives you more control over the disk cleanup process compared to using command line options.

Using Disk Cleanup PowerShell Cmdlets

Windows includes several PowerShell cmdlets that can be used to launch and run Disk Cleanup. The key cmdlets are:

  • Get-CleanmgrStatus – This displays information about disk space that can be recovered by running Disk Cleanup. It shows space usage for different file types like temporary files, system files, update files, etc. This allows you to see cleanup options before running Disk Cleanup.
  • Start-Cleanmgr – This launches the Disk Cleanup utility for a specified drive. You can use the -Parameter parameter to specify which file types to clean up. For example, ‘Start-Cleanmgr -Parameter TEMPFILES’ will launch Disk Cleanup and select the ‘Temporary Files’ option.

Some key points about using these PowerShell cmdlets for Disk Cleanup:

  • Use Get-CleanmgrStatus first to see disk space that can be recovered before running cleanup. This helps select cleanup options.
  • Use Start-Cleanmgr and specify -Parameter to launch Disk Cleanup on a drive and clean up specific file types.
  • No user interaction is required when using the cmdlets, so it can be fully automated.
  • Helpful for scheduling regular Disk Cleanup jobs using Task Scheduler.

So in summary, Get-CleanmgrStatus and Start-Cleanmgr provide a programmatic way to check disk space usage and run Disk Cleanup from PowerShell. This is useful for automation and scheduling disk cleanup.1

Cleaning Specific File Types

You can target specific file types for cleaning in PowerShell using the Remove-Item cmdlet. This allows you to delete temporary files, downloads, logs, and other unneeded file types.

For example, to recursively delete all .tmp files in the C:\Temp folder:

Remove-Item -Path C:\Temp\*.tmp -Recurse -Force

The -Path parameter specifies the folder path containing the files to delete. The -Recurse flag deletes the specified files recursively in all subfolders. And the -Force parameter forces deletion without prompting for confirmation.

Other useful Remove-Item parameters include:

  • -Include – Only delete files matching specified patterns.
  • -Exclude – Exclude files matching specified patterns.
  • -WhatIf – Show what would be deleted but don’t actually delete.

So PowerShell provides granular control over deleting specific file types across folders and drives. Just be cautious when using -Recurse and -Force to avoid accidentally deleting important files.

Source: https://stackoverflow.com/questions/37355717/delete-specific-files-on-all-remote-computers-joined-to-the-domain

Cleaning System Files

One way to clean up system files on Windows is by using the Dism.exe tool with specific parameters. Dism.exe (Deployment Image Servicing and Management) is a command-line tool that allows you to service Windows images. The following command can be used to initiate a system file cleanup:

Dism.exe /Online /Cleanup-Image /StartComponentCleanup

The /Online parameter indicates that this will run against the currently running operating system. /Cleanup-Image tells DISM to clean up the component store and remove any unnecessary files. Finally, /StartComponentCleanup initiates the cleanup process.

This will scan the component store for any files that are no longer needed by the currently installed version of Windows and safely remove them. Over time, outdated and unused files can build up, so running this command periodically helps reclaim space and keep the OS optimized. It can remove gigabytes of data from long-running Windows installations.

Dism.exe provides other options as well, such as targeting specific versions of Windows components to remove. Overall, Dism.exe with the /Cleanup-Image parameter provides an easy way for administrators to automate the cleanup of Windows system files from the command line or scripts.

Automating Disk Cleanup

Automating disk cleanup is useful for freeing disk space on a regular schedule without needing manual intervention. There are a couple main ways to automate disk cleanup in Windows:

Schedule Disk Cleanup with Task Scheduler

You can use Task Scheduler to run the Disk Cleanup tool on a set recurrence. To do this:

  1. Open Task Scheduler and create a new Basic Task
  2. Set the trigger to recur daily, weekly, or whatever schedule you want
  3. Add an action to start the cleanmgr.exe program
  4. Specify any command line parameters as needed, such as /sagerun for full disk cleanup
  5. Save the task and it will run Disk Cleanup on schedule

This automates using the standard Disk Cleanup wizard. You can also customize cleaning with PowerShell scripts.

Create PowerShell Scripts to Customize Cleaning

For more control over automating disk cleanup, you can create PowerShell scripts. Some examples:

  • Use the Clean-Disk.ps1 script to target specific file types
  • Make a script to call the Dism.exe /online /cleanup-image /startcomponentcleanup command to clean up system files
  • Combine Disk Cleanup cmdlets like Clear-RecycleBin with other file management commands

PowerShell scripts allow full customization of disk cleanup tasks.

Disk Cleanup Alternatives

While the built-in Disk Cleanup utility covers the basics, there are alternatives that can provide more robust and automated disk cleaning features.

For Windows 10 users, the built-in Storage Sense automatically frees up space by default. Storage Sense removes temporary files, recycle bin contents, and other unnecessary data based on age and frequency of use. Users can tweak auto-deletion rules and choose whether to automatically remove content from other users.

Third-party cleaners like CCleaner offer advanced cleaning of web browser caches, logs, downloads, and other unused files. CCleaner’s automatic monitoring can detect new files to delete and includes real-time optimization. Other popular alternatives include BleachBit and Wise Disk Cleaner, which provide advanced scheduling and automating of disk cleaning tasks.

When choosing an alternative disk cleaner, it’s important to be aware of potential privacy and security concerns with third-party utilities having broad access to files. Open source options like BleachBit avoid some of these issues. Overall, Disk Cleanup plus Storage Sense provide a simple option, while other tools give more customization and hands-off automation.

Performance Impacts

Disk Cleanup can help speed up a slow computer by freeing up disk space on the hard drive. When a hard drive has a lot of unnecessary files and clutter, it takes longer for the computer to access data on the drive. By removing temporary files, outdated system files, and other items that are no longer needed, Disk Cleanup frees up space which can improve system performance and boot times [1].

However, running Disk Cleanup can temporarily slow down the computer while it searches for and removes files. This is especially true if it has been a long time since Disk Cleanup was last run, as it will have more work to do. The slowdown is only temporary during the process though, and once Disk Cleanup is finished, the freed up space results in faster system performance [2].


In summary, Disk Cleanup in PowerShell provides an easy way to free up disk space and keep your system running efficiently. The key commands are Cleanmgr for running Disk Cleanup through the graphical utility, and Clear-RecycleBin, Remove-Item, and Get-ChildItem for cleaning specific file types directly in PowerShell.

Running Disk Cleanup regularly helps free up storage space by removing unnecessary system files, temporary files, caches, downloads, and other clutter. Key benefits include:

  • Freeing up storage space on hard drives
  • Improving system performance by removing unnecessary files
  • Enhancing security by deleting temporary internet files and system logs
  • Automating cleanup with PowerShell scripts for convenience

Overall, incorporating Disk Cleanup into regular system maintenance helps optimize storage usage and system speed.