Situatie
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
Solutie
Pasi de urmat
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
Leave A Comment?