95 lines
4.7 KiB
PowerShell
95 lines
4.7 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Cleanup all temporary data from browsers, temp files and windows update files.
|
|
|
|
.DESCRIPTION
|
|
This script can be scheduled or can be triggered manually in order to cleanup all temp folders inside of windows.
|
|
This includes the following folder:
|
|
- C:\Temp
|
|
- C:\Windows\Temp
|
|
- C:\Windows\SoftwareDistribution\Downloads
|
|
- Caching from Google Chrome
|
|
- Caching from Firefox
|
|
- Caching from Internet Explorer
|
|
- Caching from Microsoft Edge
|
|
- Emtpy Recycle bin
|
|
|
|
.OUTPUTS
|
|
This script will clean all temporary files and folders from the computer.
|
|
|
|
Errorcodes:
|
|
n/a
|
|
|
|
.EXAMPLE
|
|
Win - Cleanup cache and temp.ps1
|
|
|
|
.NOTES
|
|
Author: D.de Kooker <info@dcomputers.nl>
|
|
Source: n/a
|
|
|
|
.CHANGELOG
|
|
15-09-2023 - Initial script.
|
|
16-09-2023 - Added total space saved output at the end of the script.
|
|
#>
|
|
|
|
#region Parameters
|
|
|
|
#endregion
|
|
|
|
#region functions
|
|
function Cleanup-AllTemps {
|
|
$AllUsers = Get-ChildItem -Path "C:\Users" -Directory
|
|
foreach ($user in $Allusers){
|
|
Remove-Item -Path "C:\Users\$user\AppData\Local\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
}
|
|
|
|
Remove-Item -Path "C:\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -Path "C:\Windows\SoftwareDistribution\Download\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
}
|
|
|
|
function Cleanup-BrowserCaches {
|
|
$AllUsers = Get-ChildItem -Path "C:\Users" -Directory
|
|
foreach ($user in $AllUsers) {
|
|
#Firefox
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*.*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache2\entries\*.*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\thumbnails\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
# Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\cookies.sqlite" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\webappsstore.sqlite" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default\chromeappsstore.sqlite" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
|
|
#Chrome
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cache2\entries\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
# Remove-Item -path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cookies" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Media Cache" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
|
|
#Internet Explorer
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Microsoft\Windows\WER\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
|
|
#Microsoft Edge
|
|
Remove-Item -path "C:\Users\$user\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#Script run
|
|
$Starting_Diskspace = (Get-PSDrive C).Used / 1GB
|
|
|
|
Write-Host "Starting cleanup"
|
|
Cleanup-AllTemps
|
|
Cleanup-BrowserCaches
|
|
Clear-RecycleBin -Force -EA SilentlyContinue -Verbose
|
|
Write-Host "Cleanup completed.. Retrieving statistics.."
|
|
|
|
$Ending_Diskspace = (Get-PSDrive C).Used / 1GB
|
|
$SpaceSaved = [math]::Round(($Starting_Diskspace - $Ending_Diskspace), 2)
|
|
|
|
# Display the results
|
|
Write-Host "Used space before cleanup: $($Starting_Diskspace.ToString("N2")) GB"
|
|
Write-Host "Used space after cleanup: $($Ending_Diskspace.ToString("N2")) GB"
|
|
Write-Host "Space saved: $($SpaceSaved.ToString("N2")) GB" |