Maintenance-Clear-Cache.ps1
|
<#
.SYNOPSIS Clears various caches on the system and empties the Recycle Bin. .DESCRIPTION This function clears temporary files and caches from different locations on the system, including user-specific folders and system-wide cache directories. It also empties the Recycle Bin to free up disk space. .PARAMETER CachePaths Specify an array of cache paths to clear. Default paths are used if not provided. .PARAMETER LogPath Specify a log file path to record the script's actions and any errors. .EXAMPLE Maintenance-Clear-Cache .NOTES File Name : Clear-Caches.ps1 #> function Maintenance-Clear-Cache { [CmdletBinding()] param ( [string[]] $CachePaths = @( "C:\Users\$env:USERNAME\AppData\Local\Temp\*", "C:\Windows\Temp\*", "C:\Users\$env:USERNAME\AppData\Local\Microsoft\Windows\Explorer\*", "C:\Users\$env:USERNAME\AppData\Roaming\Microsoft\Windows\Recent\*", "C:\Windows\prefetch\*", "C:\Windows\SoftwareDistribution\*", "C:\Users\$env:USERNAME\AppData\Local\Microsoft\Windows\INetCache\*", "C:\Users\$env:USERNAME\AppData\Local\Packages\Microsoft.MicrosoftEdge_<RandomCharacters>\AC\MicrosoftEdge\Cache\*", "C:\Users\$env:USERNAME\AppData\Local\Google\Chrome\User Data\Default\Cache\*", "C:\Users\$env:USERNAME\AppData\Local\Mozilla\Firefox\Profiles\<ProfileFolder>\cache2\*", "C:\Users\$env:USERNAME\AppData\Local\Adobe\OOBE\PDApp\cache\*", "C:\Users\$env:USERNAME\AppData\Local\Packages\Microsoft.WindowsStore_<RandomCharacters>\LocalState\Cache\*", "C:\Users\$env:USERNAME\AppData\LocalLow\Sun\Java\Deployment\cache\*" ), [string] $LogPath = "C:\Users\User\Documents\PowerShell-Logs" ) try { foreach ($path in $CachePaths) { Remove-Item -Path $path -Force -Recurse } Clear-RecycleBin -Force $successMessage = "Caches cleared successfully." Write-Host $successMessage -ForegroundColor Green if ($LogPath) { $successMessage | Out-File -Append -FilePath $LogPath } } catch [System.IO.IOException] { $errorMessage = "Error clearing caches: $_" Write-Host $errorMessage -ForegroundColor Red if ($LogPath) { $errorMessage | Out-File -Append -FilePath $LogPath } } } |