-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathScheduledSystemCleanup.ps1
More file actions
82 lines (68 loc) · 2.21 KB
/
ScheduledSystemCleanup.ps1
File metadata and controls
82 lines (68 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<#
.SYNOPSIS
Performs a scheduled cleanup of temporary files, recycle bin, and browser caches.
.DESCRIPTION
This script is designed to clean up unnecessary files from the system, including:
- Temporary files
- Recycle bin contents
- Browser caches (Microsoft Edge, Google Chrome, and Mozilla Firefox)
.NOTES
Author: Emanuele Bartolesi
Version: 1.0
Created: 2024-11-17
.EXAMPLE
./ScheduledSystemCleanup.ps1
#>
# Enable verbose output for detailed logging
[CmdletBinding()]
param (
[switch]$VerboseLogging
)
# Define paths for cleanup
$TempPaths = @(
"$env:TEMP\*",
"$env:LOCALAPPDATA\Temp\*"
)
$BrowserCachePaths = @(
"$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache\*",
"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\*",
"$env:APPDATA\Mozilla\Firefox\Profiles\*\cache2\entries\*"
)
# Function to remove files from a given path
function Clear-Files {
param (
[string[]]$Paths
)
foreach ($Path in $Paths) {
Write-Verbose "Clearing files from: $Path"
if (Test-Path $Path) {
Get-ChildItem -Path $Path -Force -Recurse | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Write-Verbose "Files removed from: $Path"
} else {
Write-Verbose "Path not found: $Path"
}
}
}
# Function to empty the recycle bin
function Clear-RecycleBin {
Write-Verbose "Emptying the recycle bin..."
try {
(New-Object -ComObject Shell.Application).Namespace(10).Items() | ForEach-Object { $_.InvokeVerb("delete") }
Write-Verbose "Recycle bin emptied."
} catch {
Write-Warning "Failed to clear the recycle bin: $_"
}
}
# Main cleanup process
Write-Host "Starting Scheduled System Cleanup..." -ForegroundColor Green
# Clear temporary files
Write-Host "Clearing temporary files..."
Clear-Files -Paths $TempPaths
# Empty recycle bin
Write-Host "Emptying recycle bin..."
Clear-RecycleBin
# Clear browser caches
Write-Host "Clearing browser caches..."
Clear-Files -Paths $BrowserCachePaths
Write-Host "System cleanup completed successfully!" -ForegroundColor Green
# End of script