Disk Cleanup Using Powershell Scripts
Last Updated :
27 Sep, 2023
In this article, we will look at how to delete temporary files, recycle bin data, and run a disc cleanup program in Windows. Windows users sometimes faced low disk space errors with the error "You're running out of space on this PC. Manage storage to view usage and free up some space." When Windows prompts low disk space warning messages, we first perform some manual steps to delete temp files which are as follows:
- Clear temp file with disk cleanup and,
- Clear other temp files from different locations
- C:\Windows\Temp
- C:\Windows\Prefetch
- C:\Users\*\AppData\Local\Temp
Script to clear Cache and Temporary files on Windows Devices
Manually deleting this much data will take some time and will most likely need a lot of human intervention. Doing the aforementioned procedures manually on a single computer does not take long. But, if you are running 7 to 8 virtual machines with Windows operating system and doing a lot of technical work, there might be performance issues with your machines. Now, doing these manual actions in all the machines takes a long time. To avoid this problem, let's design a utility. This tool eliminates the need for manual intervention by deleting all data from the aforementioned places, running a disc cleanup tool, and clearing the recycle bin.
Delete Recycle Bin Data
Fetch the path from the system and create a variable $Path and assign the path. Make sure to separate the drive and the path as mentioned below.
- `Get-ChildItem` will retrieve the specified items and their child’s items from that location.
- `-ErrorAction` SilentlyContinue will ignore permission security-related issues. The Remove-Item will delete all the data in the present location. The Recurse parameter searches the Path directory its subdirectories and the Force parameter displays hidden files.
- `Write-Host` allows you to emit output to the information stream and you can specify the color of text by using the `-ForegroundColor` parameter.
- If you want to ignore some files that you do not want to delete, then you can use the `-exclude` parameter.
#1# Removing recycle bin files
# Set the path to the recycle bin on the C drive
$Path = 'C' + ':\$Recycle.Bin'
# Get all items (files and directories) within the recycle bin path, including hidden ones
Get-ChildItem $Path -Force -Recurse -ErrorAction SilentlyContinue |
# Remove the items, excluding any files with the .ini extension
Remove-Item -Recurse -Exclude *.ini -ErrorAction SilentlyContinue
# Display a success message
write-Host "All the necessary data removed from recycle bin successfully" -ForegroundColor Green
Delete Temp Data
Here, we have specified the paths where the temporary data is present. As you can see, this is explained in detail in the "Delete Recycle Bin data" section.
#2# Remove Temp files from various locations
write-Host "Erasing temporary files from various locations" -ForegroundColor Yellow
# Specify the path where temporary files are stored in the Windows Temp folder
$Path1 = 'C' + ':\Windows\Temp'
# Remove all items (files and directories) from the Windows Temp folder
Get-ChildItem $Path1 -Force -Recurse -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# Specify the path where temporary files are stored in the Windows Prefetch folder
$Path2 = 'C' + ':\Windows\Prefetch'
# Remove all items (files and directories) from the Windows Prefetch folder
Get-ChildItem $Path2 -Force -Recurse -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# Specify the path where temporary files are stored in the user's AppData\Local\Temp folder
$Path3 = 'C' + ':\Users\*\AppData\Local\Temp'
# Remove all items (files and directories) from the specified user's Temp folder
Get-ChildItem $Path4 -Force -Recurse -ErrorAction SilentlyContinue | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# Display a success message
write-Host "removed all the temp files successfully" -ForegroundColor Green
Note: Please note that the $Path3 has the symbol '*', which denotes 'All'. $Path3 will give you the error, you need to replace it with your system's path.
For Example C:\Users\xyx~1\AppData\Local\Temp
Perform disk Cleanup
The `Cleanmgr` command will remove unused files from the hard drive of your computer. For eg. Temp files, Internet files, downloaded files, and Recycle Bin files can all be cleared using command-line parameters. The Sagerun:1 indicates it will perform disk cleanup only once.
#3# Using Disk cleanup Tool
# Display a message indicating the usage of the Disk Cleanup tool
write-Host "Using Disk cleanup Tool" -ForegroundColor Yellow
# Run the Disk Cleanup tool with the specified sagerun parameter
cleanmgr /sagerun:1 | out-Null
# Emit a beep sound using ASCII code 7
Write-Host "$([char]7)"
# Pause the script for 5 seconds
Sleep 5
# Display a success message indicating that Disk Cleanup was successfully done
write-Host "Disk Cleanup Successfully done" -ForegroundColor Green
# Pause the script for 10 seconds
Sleep 10
To create the utility, copy the above-mentioned code and save it in the notepad with the extension .ps1 (.ps1 indicates PowerShell file).
Right-click the file and click “Run with PowerShell”. It will run this utility and will delete the data automatically without any manual intervention.
Output file
Similar Reads
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
grep command in Unix/Linux The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
Sed Command in Linux/Unix With Examples The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing.With SED, you can manipulate text files wit
9 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
ZIP command in Linux with examples In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read
screen command in Linux with Examples The screen command is an advanced terminal multiplexer that allows you to have multiple sessions within one terminal window. It's like having "tabs" in your Linux terminal â you can open, detach, switch, or resume sessions at any time without losing what you're working on. It's particularly convenie
7 min read