-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwipe-openwith-entry.ps1
More file actions
449 lines (384 loc) · 15.8 KB
/
wipe-openwith-entry.ps1
File metadata and controls
449 lines (384 loc) · 15.8 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<#
.SYNOPSIS
Removes dead "Open With" context menu entries from Windows Explorer.
.DESCRIPTION
This script identifies and removes registry entries for applications that appear in
the "Open With" context menu but whose executables no longer exist on the filesystem.
Can also force-remove entries for a specific application name.
.PARAMETER ApplicationName
Optional. The executable name to search for and remove (e.g., "photoshop.exe").
If not specified, scans all Open With entries and removes those pointing to non-existent files.
.PARAMETER Force
When specified with ApplicationName, removes all entries for that app without checking
if the executable exists. Useful for stubborn entries.
.PARAMETER ScanAll
Scans all Open With entries and removes those pointing to non-existent executables.
.PARAMETER WhatIf
Shows what would be removed without actually removing it.
.EXAMPLE
.\wipe-openwith-entry.ps1 -ScanAll
Scans all Open With entries and removes dead links.
.EXAMPLE
.\wipe-openwith-entry.ps1 -ApplicationName "photoshop.exe" -Force
Removes all references to photoshop.exe regardless of whether it exists.
.EXAMPLE
.\wipe-openwith-entry.ps1 -ApplicationName "oldapp.exe" -WhatIf
Shows what would be removed for oldapp.exe without making changes.
#>
[CmdletBinding(SupportsShouldProcess = $true, DefaultParameterSetName = 'ScanAll')]
param(
[Parameter(ParameterSetName = 'ByName', Mandatory = $true)]
[string]$ApplicationName,
[Parameter(ParameterSetName = 'ByName')]
[switch]$Force,
[Parameter(ParameterSetName = 'ScanAll')]
[switch]$ScanAll
)
# Check for admin privileges
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Warning "Not running as administrator. System-wide (HKLM) entries will be skipped."
}
$script:entriesToRemove = @()
$script:processedPaths = @{}
function Test-ExecutableExists {
param([string]$ExeName)
# Try to find the executable in PATH
$found = Get-Command $ExeName -ErrorAction SilentlyContinue
if ($found) { return $true }
# Check common installation directories
$searchPaths = @(
"$env:ProgramFiles",
"${env:ProgramFiles(x86)}",
"$env:LocalAppData\Programs"
)
foreach ($basePath in $searchPaths) {
if (Test-Path $basePath) {
$found = Get-ChildItem -Path $basePath -Filter $ExeName -Recurse -ErrorAction SilentlyContinue -Depth 3 | Select-Object -First 1
if ($found) { return $true }
}
}
return $false
}
function Get-ExecutableFromProgId {
param([string]$ProgId)
# Try to resolve ProgId to actual executable
$shellOpenPath = "Registry::HKEY_CLASSES_ROOT\$ProgId\shell\open\command"
if (Test-Path $shellOpenPath) {
try {
$command = (Get-ItemProperty -Path $shellOpenPath -Name '(Default)' -ErrorAction SilentlyContinue).'(Default)'
if ($command) {
# Extract executable from command (handle quotes and arguments)
if ($command -match '^"([^"]+)"' -or $command -match '^([^\s]+)') {
return $matches[1]
}
}
} catch {}
}
return $null
}
function Add-EntryForRemoval {
param(
[string]$Path,
[string]$Type,
[string]$Reason,
[string]$Property = $null,
[string]$Value = $null,
[string]$Extension = $null
)
# Avoid duplicates
$key = "$Path|$Property"
if ($script:processedPaths.ContainsKey($key)) { return }
$script:processedPaths[$key] = $true
$script:entriesToRemove += [PSCustomObject]@{
Path = $Path
Type = $Type
Reason = $Reason
Property = $Property
Value = $Value
Extension = $Extension
}
}
function Process-OpenWithList {
param(
[string]$Path,
[string]$Extension,
[string]$TargetApp = $null,
[bool]$CheckExists = $true
)
if (-not (Test-Path $Path)) { return }
try {
$values = Get-ItemProperty -Path $Path -ErrorAction SilentlyContinue
if (-not $values) { return }
$valuesToRemove = @()
$mruList = $values.MRUList
# Process each value (typically named a, b, c, etc.)
$props = $values.PSObject.Properties | Where-Object {
$_.Name -notlike "PS*" -and $_.Name -ne "MRUList"
}
foreach ($prop in $props) {
$exeName = $prop.Value
if (-not $exeName) { continue }
$shouldRemove = $false
$reason = ""
if ($TargetApp) {
# Specific app mode
if ($exeName -like "*$TargetApp*") {
$shouldRemove = $true
$reason = "Matches target application '$TargetApp'"
}
}
elseif ($CheckExists) {
# Scan mode - check if executable exists
if (-not (Test-ExecutableExists -ExeName $exeName)) {
$shouldRemove = $true
$reason = "Executable '$exeName' not found on system"
}
}
if ($shouldRemove) {
Add-EntryForRemoval -Path $Path -Type "Registry Value" `
-Reason $reason -Property $prop.Name -Value $exeName -Extension $Extension
$valuesToRemove += $prop.Name
}
}
# If we removed values, we need to update MRUList
if ($valuesToRemove.Count -gt 0 -and $mruList) {
$newMRUList = $mruList
foreach ($val in $valuesToRemove) {
$newMRUList = $newMRUList -replace $val, ''
}
if ($newMRUList -ne $mruList) {
Add-EntryForRemoval -Path $Path -Type "Registry Value" `
-Reason "Update MRUList after removing dead entries" `
-Property "MRUList" -Value $newMRUList -Extension $Extension
}
}
} catch {
Write-Verbose "Error processing OpenWithList at ${Path}: $($_.Exception.Message)"
}
}
function Process-ApplicationEntry {
param(
[string]$AppKeyPath,
[string]$AppName,
[string]$TargetApp = $null,
[bool]$CheckExists = $true
)
$shouldRemove = $false
$reason = ""
if ($TargetApp) {
if ($AppName -like "*$TargetApp*") {
$shouldRemove = $true
$reason = "Application entry for '$TargetApp'"
}
}
elseif ($CheckExists) {
# Try to find the executable path from the application entry
$commandPath = Join-Path $AppKeyPath "shell\open\command"
if (Test-Path $commandPath) {
try {
$command = (Get-ItemProperty -Path $commandPath -Name '(Default)' -ErrorAction SilentlyContinue).'(Default)'
if ($command) {
# Extract path from command
$exePath = $null
if ($command -match '^"([^"]+)"') {
$exePath = $matches[1]
}
elseif ($command -match '^([^\s]+)') {
$exePath = $matches[1]
}
if ($exePath -and -not (Test-Path $exePath)) {
$shouldRemove = $true
$reason = "Executable path not found: $exePath"
}
}
} catch {}
}
else {
# No command found, check if the app name itself exists
if (-not (Test-ExecutableExists -ExeName $AppName)) {
$shouldRemove = $true
$reason = "Application '$AppName' not found on system"
}
}
}
if ($shouldRemove) {
Add-EntryForRemoval -Path $AppKeyPath -Type "Registry Key" -Reason $reason
}
}
# Main execution
if ($ApplicationName) {
Write-Host "Searching for entries related to '$ApplicationName'..." -ForegroundColor Yellow
$targetApp = $ApplicationName
$checkExists = -not $Force
if ($Force) {
Write-Host "Force mode: Will remove all entries regardless of executable existence" -ForegroundColor Yellow
}
}
else {
Write-Host "Scanning all 'Open With' entries for dead links..." -ForegroundColor Yellow
$targetApp = $null
$checkExists = $true
}
# 1. Process HKCU:\Software\Classes\Applications
Write-Verbose "Scanning HKCU:\Software\Classes\Applications..."
$userAppsPath = "HKCU:\Software\Classes\Applications"
if (Test-Path $userAppsPath) {
$apps = Get-ChildItem -Path $userAppsPath -ErrorAction SilentlyContinue
foreach ($app in $apps) {
Process-ApplicationEntry -AppKeyPath $app.PSPath -AppName $app.PSChildName `
-TargetApp $targetApp -CheckExists $checkExists
}
}
# 2. Process HKLM:\Software\Classes\Applications (requires admin)
if ($isAdmin) {
Write-Verbose "Scanning HKLM:\Software\Classes\Applications..."
$systemAppsPath = "HKLM:\Software\Classes\Applications"
if (Test-Path $systemAppsPath) {
$apps = Get-ChildItem -Path $systemAppsPath -ErrorAction SilentlyContinue
foreach ($app in $apps) {
Process-ApplicationEntry -AppKeyPath $app.PSPath -AppName $app.PSChildName `
-TargetApp $targetApp -CheckExists $checkExists
}
}
}
# 3. Process FileExts OpenWithList entries
Write-Verbose "Scanning file extension Open With lists..."
$fileExtsPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts"
if (Test-Path $fileExtsPath) {
$extensions = Get-ChildItem -Path $fileExtsPath -ErrorAction SilentlyContinue
foreach ($ext in $extensions) {
$openWithListPath = Join-Path $ext.PSPath "OpenWithList"
Process-OpenWithList -Path $openWithListPath -Extension $ext.PSChildName `
-TargetApp $targetApp -CheckExists $checkExists
# Also check OpenWithProgids
$openWithProgidsPath = Join-Path $ext.PSPath "OpenWithProgids"
if (Test-Path $openWithProgidsPath) {
try {
$progids = Get-ItemProperty -Path $openWithProgidsPath -ErrorAction SilentlyContinue
if ($progids) {
$props = $progids.PSObject.Properties | Where-Object { $_.Name -notlike "PS*" }
foreach ($prop in $props) {
$shouldRemove = $false
if ($targetApp -and $prop.Name -like "*$targetApp*") {
$shouldRemove = $true
}
elseif ($checkExists) {
$exePath = Get-ExecutableFromProgId -ProgId $prop.Name
if ($exePath -and -not (Test-Path $exePath)) {
$shouldRemove = $true
}
}
if ($shouldRemove) {
Add-EntryForRemoval -Path $openWithProgidsPath -Type "Registry Value" `
-Reason "Dead ProgId reference" -Property $prop.Name `
-Value $prop.Value -Extension $ext.PSChildName
}
}
}
} catch {
Write-Verbose "Error processing OpenWithProgids for $($ext.PSChildName)"
}
}
}
}
# 4. Process SystemFileAssociations (requires admin)
if ($isAdmin) {
Write-Verbose "Scanning system file associations..."
$systemAssocPath = "HKLM:\Software\Classes\SystemFileAssociations"
if (Test-Path $systemAssocPath) {
$associations = Get-ChildItem -Path $systemAssocPath -ErrorAction SilentlyContinue
foreach ($assoc in $associations) {
$openWithListPath = Join-Path $assoc.PSPath "OpenWithList"
Process-OpenWithList -Path $openWithListPath -Extension $assoc.PSChildName `
-TargetApp $targetApp -CheckExists $checkExists
}
}
}
# Display results
if ($entriesToRemove.Count -eq 0) {
Write-Host "`nNo dead or matching entries found." -ForegroundColor Green
return
}
Write-Host "`nFound $($entriesToRemove.Count) entries to remove:" -ForegroundColor Yellow
Write-Host ""
# Group by extension for better readability
$grouped = $entriesToRemove | Group-Object Extension | Sort-Object Name
foreach ($group in $grouped) {
if ($group.Name) {
Write-Host "Extension: $($group.Name)" -ForegroundColor Cyan
}
else {
Write-Host "Application Registrations:" -ForegroundColor Cyan
}
foreach ($entry in $group.Group) {
Write-Host " [$($entry.Type)]" -ForegroundColor DarkCyan -NoNewline
if ($entry.Property) {
Write-Host " Property '$($entry.Property)' = '$($entry.Value)'" -ForegroundColor Gray
}
else {
Write-Host " $($entry.Path -replace '.*\\', '')" -ForegroundColor Gray
}
Write-Host " Path: $($entry.Path)" -ForegroundColor DarkGray
Write-Host " Reason: $($entry.Reason)" -ForegroundColor DarkGray
}
Write-Host ""
}
# Confirm and remove
if (-not $PSCmdlet.ShouldProcess("$($entriesToRemove.Count) registry entries", "Remove")) {
Write-Host "No changes were made." -ForegroundColor Green
return
}
Write-Host "Removing entries..." -ForegroundColor Yellow
$removedCount = 0
$failedCount = 0
# Group removals to handle MRUList updates properly
$removalsByPath = $entriesToRemove | Group-Object Path
foreach ($group in $removalsByPath) {
$path = $group.Name
$entries = $group.Group
try {
if (-not (Test-Path $path)) {
Write-Verbose "Path no longer exists: $path"
continue
}
# Handle registry keys (remove entire key)
$keyRemovals = $entries | Where-Object { $_.Type -eq "Registry Key" }
if ($keyRemovals) {
Remove-Item -Path $path -Recurse -Force -ErrorAction Stop
Write-Host " [OK] Removed: $path" -ForegroundColor Green
$removedCount += $keyRemovals.Count
continue
}
# Handle registry values (remove specific properties)
$valueRemovals = $entries | Where-Object { $_.Type -eq "Registry Value" }
# Handle MRUList specially
$mruUpdate = $valueRemovals | Where-Object { $_.Property -eq "MRUList" }
$otherValues = $valueRemovals | Where-Object { $_.Property -ne "MRUList" }
foreach ($entry in $otherValues) {
Remove-ItemProperty -Path $path -Name $entry.Property -Force -ErrorAction Stop
Write-Host " [OK] Removed '$($entry.Property)' from: $path" -ForegroundColor Green
$removedCount++
}
# Update MRUList last
if ($mruUpdate) {
Set-ItemProperty -Path $path -Name "MRUList" -Value $mruUpdate.Value -Force -ErrorAction Stop
Write-Host " [OK] Updated MRUList in: $path" -ForegroundColor Green
$removedCount++
}
}
catch {
Write-Warning " [FAIL] Failed to process $path : $($_.Exception.Message)"
$failedCount += $entries.Count
}
}
Write-Host ""
Write-Host "Summary:" -ForegroundColor Cyan
Write-Host " Removed: $removedCount entries" -ForegroundColor Green
if ($failedCount -gt 0) {
Write-Host " Failed: $failedCount entries" -ForegroundColor Red
Write-Host " Try running as administrator for system-wide entries" -ForegroundColor Yellow
}
Write-Host "`nRefresh Windows Explorer to see changes:" -ForegroundColor Yellow
Write-Host " - Restart Explorer: taskkill /f /im explorer.exe; start explorer.exe" -ForegroundColor White
Write-Host " - Or restart your computer" -ForegroundColor White