Repository navigation
Expand file tree
/
Copy pathWin10-11_Professional-Cleanup.ps1
More file actions
194 lines (165 loc) · 6.97 KB
/
Copy pathWin10-11_Professional-Cleanup.ps1
File metadata and controls
194 lines (165 loc) · 6.97 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<#
Windows 10/11 – Admin Cleanup Script
------------------------------------
Sicheres Bereinigungsskript für produktive Systeme ohne Neustart.
Entfernt temporäre Dateien, Caches und Logs,
prüft und repariert Windows-Komponenten (DISM/SFC).
Nutzer können währenddessen weiterarbeiten.
Optionale Parameter:
-IncludePrefetch
Prefetch-Ordner leeren (Programme starten beim ersten Mal etwas langsamer)
-BrowserCacheHard
Browser-Caches löschen (Edge, Chrome, Firefox),
Cookies und Passwörter bleiben erhalten
-ResetWU
Windows Update-Cache zurücksetzen
(Updates werden ggf. neu geladen)
-DryRun
Nur Simulation – nichts wird gelöscht,
nur Vorgänge werden angezeigt
-KeepLogsDays <Zahl>
Anzahl Tage, die Logs in C:\ProgramData\SSIG\Cleanup aufbewahrt werden
Beispiele:
Standardlauf:
.\Win10-11_Admin-Cleanup.ps1
Mit mehreren Optionen:
.\Win10-11_Admin-Cleanup.ps1 -IncludePrefetch -BrowserCacheHard -ResetWU
#>
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')]
param(
[switch]$IncludePrefetch,
[switch]$ResetWU,
[switch]$BrowserCacheHard,
[switch]$DryRun,
[int]$KeepLogsDays = 14
)
# --- Setup & Logging ---
$ErrorActionPreference = 'SilentlyContinue'
$PSDefaultParameterValues['*:ErrorAction'] = 'SilentlyContinue'
$StartTime = Get-Date
$LogRoot = "$env:ProgramData\SSIG\Cleanup"
$null = New-Item -ItemType Directory -Force -Path $LogRoot
$LogFile = Join-Path $LogRoot ("Cleanup_{0:yyyy-MM-dd_HH-mm-ss}.log" -f $StartTime)
Start-Transcript -Path $LogFile -Force | Out-Null
function Write-Log {
param([string]$Message,[ValidateSet('INFO','WARN','ERROR')][string]$Level='INFO')
$line = "{0:u} [{1}] {2}" -f (Get-Date), $Level, $Message
Write-Host $line
Add-Content -Path $LogFile -Value $line
}
function Get-FreeBytes($drive='C') {
try { (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='${drive}:'").FreeSpace } catch { 0 }
}
function Remove-PathSafe {
param([string]$Path)
if (Test-Path -LiteralPath $Path) {
if ($DryRun) { Write-Log "DRYRUN: würde löschen -> $Path" }
else {
try {
Get-ChildItem -LiteralPath $Path -Force -Recurse | ForEach-Object { try { $_.Attributes='Normal' } catch {} }
Remove-Item -LiteralPath $Path -Recurse -Force
Write-Log "Gelöscht: $Path"
} catch { Write-Log "Fehler beim Löschen: $Path => $($_.Exception.Message)" 'WARN' }
}
} else { Write-Log "Pfad nicht gefunden: $Path" 'INFO' }
}
Write-Log "== Cleanup gestartet auf $env:COMPUTERNAME | User: $env:USERNAME | DryRun=$DryRun =="
$FreeBefore = Get-FreeBytes 'C'
# --- 1) Benutzer-Temp ---
Write-Log "[1] Benutzer-Temp & INetCache"
Get-ChildItem 'C:\Users' -Directory -Force | ForEach-Object {
Remove-PathSafe "$($_.FullName)\AppData\Local\Temp\*"
Remove-PathSafe "$($_.FullName)\AppData\Local\Microsoft\Windows\INetCache\*"
}
# --- 2) System-Temp ---
Write-Log "[2] System-Temp"
Remove-PathSafe "$env:windir\Temp\*"
Remove-PathSafe "$env:TEMP\*"
# --- 3) Delivery Optimization Cache ---
Write-Log "[3] Delivery Optimization Cache"
Remove-PathSafe "C:\ProgramData\Microsoft\Windows\DeliveryOptimization\Cache\*"
# --- 4) Windows-Logs & Setup-Logs ---
Write-Log "[4] Windows-Logs"
Remove-PathSafe "$env:windir\Logs\CBS\*"
Remove-PathSafe "$env:windir\Logs\DISM\*"
Remove-PathSafe "$env:windir\Panther\*"
Remove-PathSafe "$env:windir\inf\*.log"
# --- 5) Shader-, Thumbnail- und Icon-Cache ---
Write-Log "[5] Shader-, Thumbnail- und Icon-Cache"
Remove-PathSafe "$env:localappdata\Microsoft\Windows\ShaderCache\*"
Remove-PathSafe "$env:localappdata\Microsoft\Windows\Explorer\thumbcache_*.db"
Remove-PathSafe "$env:localappdata\IconCache.db"
# --- 6) Cleanmgr (silent) ---
Write-Log "[6] Cleanmgr SAGERUN (silent)"
$SageId = 20250
$handlers = @(
'Temporary Files','Temporary Setup Files','Old ChkDsk Files','Setup Log Files',
'Windows Error Reporting Files','DirectX Shader Cache','Thumbnail Cache',
'Update Cleanup','Device Driver Packages','Delivery Optimization Files'
)
$vcBase = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches'
foreach ($h in $handlers) {
$path = Join-Path $vcBase $h
if (Test-Path $path -PathType Container -ErrorAction SilentlyContinue) {
if (-not $DryRun) { New-ItemProperty -Path $path -Name "StateFlags$SageId" -Value 2 -PropertyType DWord -Force | Out-Null }
}
}
if (-not $DryRun) { & cleanmgr.exe /sagerun:$SageId }
# --- 7) DISM StartComponentCleanup ---
Write-Log "[7] DISM StartComponentCleanup"
if (-not $DryRun) { & dism.exe /Online /Cleanup-Image /StartComponentCleanup /Quiet }
# --- 8) Optional: Prefetch ---
if ($IncludePrefetch) {
Write-Log "[8] Prefetch leeren"
if (-not $DryRun) {
try { Stop-Service SysMain -Force } catch {}
Remove-PathSafe "$env:windir\Prefetch\*"
try { Start-Service SysMain } catch {}
}
}
# --- 9) Optional: Windows Update Reset ---
if ($ResetWU) {
Write-Log "[9] Windows Update-Cache zurücksetzen"
if (-not $DryRun) {
net stop wuauserv
net stop bits
net stop cryptsvc
Rename-Item "$env:windir\SoftwareDistribution" "SoftwareDistribution.old" -Force -ErrorAction SilentlyContinue
Rename-Item "$env:windir\System32\catroot2" "catroot2.old" -Force -ErrorAction SilentlyContinue
net start wuauserv
net start bits
net start cryptsvc
}
}
# --- 10) Optional: Browser-Caches ---
if ($BrowserCacheHard) {
Write-Log "[10] Browser-Caches leeren"
$paths = @(
"$env:localappdata\Microsoft\Edge\User Data\Default\Cache\*",
"$env:localappdata\Google\Chrome\User Data\Default\Cache\*",
"$env:appdata\Mozilla\Firefox\Profiles\*\cache2\*"
)
foreach ($p in $paths) { Remove-PathSafe $p }
}
# --- 11) DeepRepair (immer aktiv) ---
$DeepRepairStart = Get-Date
Write-Log "[11] DeepRepair gestartet"
if (-not $DryRun) {
& dism.exe /Online /Cleanup-Image /ScanHealth
& dism.exe /Online /Cleanup-Image /RestoreHealth
& sfc.exe /scannow
}
$DeepRepairDur = (Get-Date) - $DeepRepairStart
Write-Log ("[11] DeepRepair beendet – Dauer: {0:mm\:ss}" -f $DeepRepairDur)
# --- 12) DNS-Cache ---
Write-Log "[12] DNS-Cache leeren"
if (-not $DryRun) { ipconfig /flushdns | Out-Null }
# --- Abschluss ---
$FreeAfter = Get-FreeBytes 'C'
$freed = [math]::Max(0, ($FreeAfter - $FreeBefore))
$freedMB = [math]::Round($freed/1MB,2)
$dur = (Get-Date) - $StartTime
Write-Log ("== Fertig | Gesamtdauer: {0:mm\:ss} | Vorher: {1:N0} MB frei | Nachher: {2:N0} MB frei | Gewinn: {3:N2} MB ==" -f $dur, ($FreeBefore/1MB), ($FreeAfter/1MB), $freedMB)
# Log-Hygiene
Get-ChildItem $LogRoot -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$KeepLogsDays) } | Remove-Item -Force -ErrorAction SilentlyContinue
Stop-Transcript | Out-Null