Repository navigation
Expand file tree
/
Copy pathAutoCacheDeleted.ps1
More file actions
95 lines (69 loc) · 3.26 KB
/
Copy pathAutoCacheDeleted.ps1
File metadata and controls
95 lines (69 loc) · 3.26 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
83
84
85
86
87
88
89
90
91
92
93
94
95
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
'
$console = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($console, 5) # 5 = SW_SHOW
shutdown /a
Write-Host "=== SYSTEM SHUTDOWN CACHE CLEANER ===" -ForegroundColor Cyan
function Clear-TempWithTracking {
param (
[string]$DirectoryPath
)
$initialCount = @(Get-ChildItem -Path $DirectoryPath -Recurse -Force -ErrorAction SilentlyContinue).Count
if ($initialCount -eq 0) {
Write-Host "Directory $DirectoryPath is already empty" -ForegroundColor Yellow
return $true
}
Write-Host "Deleting $initialCount files from $DirectoryPath..."
$job = Start-Job -ScriptBlock {
param($path)
Get-ChildItem -Path $path -Recurse -Force | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
} -ArgumentList $DirectoryPath
do {
$currentCount = @(Get-ChildItem -Path $DirectoryPath -Recurse -Force -ErrorAction SilentlyContinue).Count
$remaining = $initialCount - $currentCount
$percentComplete = if ($initialCount -gt 0) {
[math]::Min(100, [math]::Max(0, ($remaining / $initialCount * 100)))
} else {
100
}
Write-Progress -Activity "Deleting files" `
-Status "Remaining: $currentCount files ($remaining of $initialCount processed)" `
-PercentComplete $percentComplete
if ($currentCount -eq 0) {
Write-Host "All files deleted successfully" -ForegroundColor Green
return $true
}
Start-Sleep -Milliseconds 500
} while ($job.State -eq 'Running')
return ($currentCount -eq 0)
}
$tempPaths = @(
"$env:TEMP", # Временные файлы пользователя
"C:\Windows\Temp", # Временные файлы системы
"C:\Windows\Prefetch", # Данные для оптимизации запуска программ
"C:\Windows\SoftwareDistribution\Download", # Кеш загрузки центра обновлений windows
"C:\ProgramData\Microsoft\Windows\WER", # Файлы отчетов об ошибке
"$env:LOCALAPPDATA\Microsoft\Windows\DeliveryOptimization", # Кеш оптимизации доставки
"C:\ProgramData\Microsoft\Windows Defender\Scans\History", # История сканирования защитником windows
"$env:LOCALAPPDATA\Microsoft\Windows\FontCache", # Кеш шрифтов
"$env:LOCALAPPDATA\Microsoft\Windows\INetCache" # Интернет кеш (IE/Edge legacy)
)
$allCleared = $true
foreach ($path in $tempPaths) {
if (Test-Path $path) {
Write-Host "`nProcessing directory: $path"
if (-not (Clear-TempWithTracking -DirectoryPath $path)) {
$allCleared = $false
}
} else {
Write-Host "`nDirectory $path does not exist"
}
}
if ($allCleared) {
Write-Host "`nAll temp directories cleaned successfully"
} else {
Write-Host "`nSome files could not be deleted (may be in use by system)"
}
shutdown /s /t 1