-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript
More file actions
279 lines (227 loc) · 8.7 KB
/
script
File metadata and controls
279 lines (227 loc) · 8.7 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<#
Script : clean-windows.ps1
Compatibilité : Windows Server / Windows 10 / 11
Auteur : herve-mtp
#>
<#
.SYNOPSIS
Script de nettoyage système.
.DESCRIPTION
Nettoie les fichiers temporaires, journaux, cache Windows Update, corbeille, etc.
Prépare une image Windows propre pour la duplication par exemple.
.NOTES
Exécuter en tant qu'administrateur.
#>
param(
[string]$LogPath = "C:\Clean-Windows\clean-Windows.log"
)
# 🔐 Vérifier les droits administrateur
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "🚫 Ce script doit être exécuté en tant qu'administrateur." -ForegroundColor Red
exit
}
# 📁 Créer le dossier de log si nécessaire
$LogDir = Split-Path $LogPath
if (-not (Test-Path $LogDir)) {
New-Item -Path $LogDir -ItemType Directory -Force | Out-Null
}
# 📝 Fonction de log
function Log {
param (
[string]$Message,
[string]$Color = "White"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$entry = "$timestamp - $Message"
Add-Content -Path $LogPath -Value $entry
Write-Host $Message -ForegroundColor $Color
}
# 🧽 Début du nettoyage
Log "`n====== DÉBUT DU NETTOYAGE SYSTÈME ======" Cyan
# --- MODULE 2 : Nettoyage fichiers temporaires et récents ---
Log "`n🧽 Nettoyage des fichiers temporaires, récents, prefetch, jump lists..."
$tempPaths = @(
"$env:SystemRoot\Temp",
"$env:TEMP",
"$env:TMP",
"$env:SystemRoot\Prefetch"
)
# Ajouter les chemins des utilisateurs
Get-ChildItem "C:\Users" -Directory -Force | ForEach-Object {
$tempPaths += "$($_.FullName)\AppData\Local\Temp"
$tempPaths += "$($_.FullName)\AppData\Roaming\Microsoft\Windows\Recent"
$tempPaths += "$($_.FullName)\AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations"
$tempPaths += "$($_.FullName)\AppData\Roaming\Microsoft\Windows\Recent\CustomDestinations"
}
foreach ($path in $tempPaths | Sort-Object -Unique) {
if (Test-Path $path) {
Log "🧹 Nettoyage : $path"
try {
Remove-Item "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Nettoyage réussi : $path" Green
} catch {
Log "❌ Erreur nettoyage : $path - $_" Red
}
}
}
# --- MODULE 3 : Vidage de la corbeille (corrigé) ---
Log "`n🗑️ Vidage de la corbeille (tous utilisateurs)..."
try {
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
Log "✅ Corbeille utilisateur vidée." Green
} catch {
Log "❌ Échec vidage corbeille utilisateur : $_" Red
}
# Corbeilles systèmes ($Recycle.Bin sur chaque volume racine)
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Free -gt 0 }
foreach ($drive in $drives) {
$recycleBinPath = "$($drive.Root)\$Recycle.Bin"
if (Test-Path $recycleBinPath) {
try {
Remove-Item "$recycleBinPath\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Corbeille système vidée sur $($drive.Name)" Green
} catch {
Log "❌ Erreur suppression $Recycle.Bin sur $($drive.Name) : $_" Red
}
}
}
# --- MODULE 4 : Nettoyage des journaux d'événements ---
Log "`n🧾 Nettoyage des journaux d’événements..."
$logs = wevtutil el
foreach ($logName in $logs) {
try {
wevtutil cl "$logName"
Log "✅ Effacé : $logName" Green
} catch {
Log "⚠️ Impossible d'effacer : $logName - $_" Yellow
}
}
# --- MODULE 5 : Suppression du cache Windows Update ---
$wuPath = "C:\Windows\SoftwareDistribution\Download"
Log "`n📦 Suppression du cache Windows Update..."
try {
Stop-Service -Name wuauserv -Force -ErrorAction SilentlyContinue
Log "🛑 Service Windows Update arrêté." Yellow
Remove-Item "$wuPath\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Cache Windows Update vidé." Green
Start-Service -Name wuauserv -ErrorAction SilentlyContinue
Log "▶️ Service Windows Update redémarré." Yellow
} catch {
Log "❌ Erreur suppression cache Windows Update : $_" Red
}
# --- MODULE 6A : Nettoyage via cleanmgr ---
if (Get-Command cleanmgr.exe -ErrorAction SilentlyContinue) {
Log "`n🧼 Exécution de cleanmgr (mode silencieux)..."
try {
cleanmgr /sageset:1 | Out-Null
cleanmgr /sagerun:1 | Out-Null
Log "✅ cleanmgr exécuté." Green
} catch {
Log "❌ Erreur cleanmgr : $_" Red
}
} else {
Log "⚠️ cleanmgr.exe non disponible sur ce système." Yellow
}
# --- MODULE 6B : Nettoyage des composants système (WinSxS) ---
Log "`n🧹 Nettoyage des composants système (WinSxS)... Cela peut prendre plusieurs minutes, soyez patient..." Yellow
try {
$dismOutput = dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase
Log "✅ DISM terminé. Composants nettoyés." Green
} catch {
Log "❌ Erreur DISM nettoyage WinSxS : $_" Red
}
# --- MODULE 6C : Lancement de Storage Sense (Windows 10/11) ---
Log "`n💾 Lancement de Storage Sense si disponible..."
try {
Start-Process "ms-settings:storagesense" -ErrorAction SilentlyContinue
Log "✅ Storage Sense lancé (paramètres ouverts)." Green
} catch {
Log "⚠️ Impossible de lancer Storage Sense automatiquement : $_" Yellow
}
# --- MODULE 7 : Nettoyage des caches navigateurs ---
Log "`n🌐 Nettoyage des caches navigateurs..."
Get-ChildItem "C:\Users" -Directory -Force | ForEach-Object {
$userProfile = $_.FullName
# Google Chrome
$chromeCache = "$userProfile\AppData\Local\Google\Chrome\User Data\Default\Cache"
if (Test-Path $chromeCache) {
try {
Remove-Item "$chromeCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Cache Chrome vidé pour $userProfile" Green
} catch {
Log "❌ Chrome (cache) : $_" Red
}
}
# Microsoft Edge
$edgeCache = "$userProfile\AppData\Local\Microsoft\Edge\User Data\Default\Cache"
if (Test-Path $edgeCache) {
try {
Remove-Item "$edgeCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Cache Edge vidé pour $userProfile" Green
} catch {
Log "❌ Edge (cache) : $_" Red
}
}
# Firefox
$firefoxPath = "$userProfile\AppData\Roaming\Mozilla\Firefox\Profiles"
if (Test-Path $firefoxPath) {
Get-ChildItem $firefoxPath -Directory | ForEach-Object {
$ffCache = "$($_.FullName)\cache2"
if (Test-Path $ffCache) {
try {
Remove-Item "$ffCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Cache Firefox vidé pour $userProfile" Green
} catch {
Log "❌ Firefox (cache) : $_" Red
}
}
}
}
# Internet Explorer
$ieCache = "$userProfile\AppData\Local\Microsoft\Windows\INetCache"
if (Test-Path $ieCache) {
try {
Remove-Item "$ieCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Cache IE vidé pour $userProfile" Green
} catch {
Log "❌ IE (cache) : $_" Red
}
}
}
# --- MODULE 8A : Nettoyage des logs Windows Defender ---
Log "`n🛡️ Nettoyage des logs Windows Defender..."
$defenderPaths = @(
"$env:ProgramData\Microsoft\Windows Defender\Scans\History\Service",
"$env:ProgramData\Microsoft\Windows Defender\Scans\History\Results",
"$env:ProgramData\Microsoft\Windows Defender\Support"
)
foreach ($path in $defenderPaths) {
if (Test-Path $path) {
try {
Remove-Item "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Logs Defender supprimés : $path" Green
} catch {
Log "❌ Erreur suppression Defender logs : $_" Red
}
}
}
# --- MODULE 8B : Nettoyage du cache Microsoft Store ---
Log "`n🏬 Nettoyage du cache Microsoft Store..."
$storeCache = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsStore_8wekyb3d8bbwe\LocalCache"
if (Test-Path $storeCache) {
try {
Remove-Item "$storeCache\*" -Recurse -Force -ErrorAction SilentlyContinue
Log "✅ Cache Microsoft Store vidé." Green
} catch {
Log "❌ Erreur suppression cache Store : $_" Red
}
}
# --- MODULE 9 : Résumé & Fin ---
Log "`n✅ NETTOYAGE TERMINÉ - SYSTÈME OPTIMISÉ AVEC SUCCÈS ✅" Cyan
# Optionnel : afficher la taille du fichier log
if (Test-Path $LogPath) {
$logSizeMB = [math]::Round((Get-Item $LogPath).Length / 1MB, 2)
Log "📄 Fichier log : $LogPath (${logSizeMB}MB)" Gray
}
# Ajout possible : Pause finale ou fermeture automatique
Log "💡 Tu peux maintenant fermer cette fenêtre ou redémarrer le système." DarkGray