fix(tweaks): update classic context menu registry workaround (#5253)

* fix(tweaks): update classic context menu registry workaround

fix(tweaks): update classic context menu registry workaround

* fix(tweaks): make classic context menu undo safe

* Complete context menu retries and unattended setup

* Keep unattended setup running after context menu errors

* Recover unattended context menu registration after partial failures

* fix(tweaks): limit classic-menu restart to the current session

---------

Co-authored-by: Chris Titus <contact@christitus.com>
Co-authored-by: Chris Titus <7896101+ChrisTitusTech@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-11-18 16:39:31 -06:00
1 parent 2095298530
commit 70c23c8064
5 files changed
+356 -16

No files matched your search

+15 -16
View File
@@ -1162,22 +1162,21 @@
],
"link": "https://winutil.christitus.com/code-reference/tweaks/z--advanced-tweaks---caution/blockadobenet"
},
"WPFTweaksRightClickMenu": {
"Content": "Right-Click Menu Previous Layout - Enable",
"Description": "Restores the classic context menu when right-clicking in File Explorer, replacing the simplified Windows 11 version.",
"category": "z__Advanced Tweaks - CAUTION",
"panel": "1",
"InvokeScript": [
"
New-Item -Path \"HKCU:\\Software\\Classes\\CLSID\\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\" -Name InprocServer32 -Value \"\" -Force
Stop-Process -Name explorer
"
],
"UndoScript": [
"Remove-Item -Path \"HKCU:\\Software\\Classes\\CLSID\\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\" -Recurse"
],
"link": "https://winutil.christitus.com/code-reference/tweaks/z--advanced-tweaks---caution/rightclickmenu"
},
"WPFTweaksRightClickMenu": {
"Content": "Right-Click Menu Previous Layout - Enable",
"Description": "Attempts to restore the classic Windows 11 File Explorer context menu on newer builds. Compatibility may vary.",
"category": "z__Advanced Tweaks - CAUTION",
"panel": "1",
"InvokeScript": [
"Set-WinUtilClassicContextMenu"
],
"UndoScript": [
"Set-WinUtilClassicContextMenu -Undo"
],
"link": "https://winutil.christitus.com/code-reference/tweaks/z--advanced-tweaks---caution/rightclickmenu"
},
"WPFTweaksDiskCleanup": {
"Content": "Disk Cleanup - Run",
"Description": "Runs Disk Cleanup on Drive C: and removes old Windows Updates.",
+2
View File
@@ -71,6 +71,8 @@ Essential Tweaks are the safest starting point for most systems. They focus on l
### Advanced Tweaks (CAUTION)
**Right-Click Menu Previous Layout - Enable** attempts to restore the classic File Explorer context menu on newer Windows 11 builds; compatibility varies. The newer registration is left unchanged if it already exists. Undo removes that registration only when WinUtil created it and it has not been customized afterward. Apply and undo restart Explorer only in the current session. Win11 Creator applies the same newer workaround during first sign-in.
**Services - Set to Manual** changes service startup settings only. Apply changes a service only while its startup type matches the Windows default recorded in the tweak. Undo restores that default only while the current startup type matches the type the tweak sets, regardless of who changed it. For example, undo restores DiagTrack, CscService, and SharedAccess even if you disabled them yourself, because this tweak also sets them to Disabled. Startup types that do not match the expected value are left alone and logged as a warning. Disabling service-host splitting is a separate **svchost.exe Splitting - Disable** advanced tweak. It is recommended for low-performance systems with 12 GB of RAM or less to reduce service-host memory overhead. It can interfere with Xbox services; use it only when you understand that tradeoff. Undo restores the Windows client threshold of 0x380000, rather than a custom threshold you previously configured. If you applied the previous Services tweak, select **svchost.exe Splitting - Disable** and run **Undo Selected Tweaks** directly; do not apply the new tweak first. Restart Windows after changing this setting.
**Windows Search Indexing - Disable** stops the Windows Search service and sets it to Disabled. That lowers background disk activity on mechanical drives, but file searches in Explorer and the Start menu become noticeably slower because Windows no longer maintains a search index. Apply changes the startup type only while it is still the default Automatic, and undo restores Automatic only while it is still Disabled; the service is stopped only while it is Disabled and started only while it is Automatic. A startup type changed elsewhere, such as Manual, is left alone and logged as a warning.
@@ -0,0 +1,79 @@
function Set-WinUtilClassicContextMenu {
param([switch]$Undo)
$clsidPath = 'HKCU:\Software\Classes\CLSID'
$legacyPath = "$clsidPath\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}"
$workaroundPath = "$clsidPath\{51668040-5e31-4023-bbe3-149163bd80f1}"
$serverPath = "$workaroundPath\InprocServer32"
$markerName = 'WinUtilClassicContextMenu'
$workaroundKey = $null
try {
$workaroundKey = Get-Item -LiteralPath $workaroundPath -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
# The newer registration is optional.
}
$marker = if ($null -ne $workaroundKey) { $workaroundKey.GetValue($markerName) }
$ownsWorkaround = $marker -is [int] -and $marker -eq 1
if ($Undo) {
# Older WinUtil builds applied the original override without an ownership marker.
try {
Remove-Item -LiteralPath $legacyPath -Recurse -Force -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
# The original override is already absent.
}
if ($ownsWorkaround) {
try {
if ($workaroundKey.ValueCount -gt 1 -or
($workaroundKey.GetSubKeyNames() | Where-Object { $_ -ne 'InprocServer32' })) {
throw "The classic context menu registry key '$workaroundPath' has additional entries and was left intact."
}
if ($workaroundKey.SubKeyCount -gt 0) {
$serverKey = Get-Item -LiteralPath $serverPath -ErrorAction Stop
if ($serverKey.SubKeyCount -gt 0 -or $serverKey.ValueCount -gt 1 -or
($serverKey.ValueCount -eq 1 -and
($serverKey.GetValueNames() -notcontains '' -or
$serverKey.GetValueKind('') -ne [Microsoft.Win32.RegistryValueKind]::String -or
$serverKey.GetValue('') -ne ''))) {
throw "The classic context menu registry key '$serverPath' has been changed and was left intact."
}
}
Remove-Item -LiteralPath $workaroundPath -Recurse -Force -ErrorAction Stop
} catch [System.Management.Automation.ItemNotFoundException] {
# The key disappeared before cleanup completed.
}
}
} else {
New-Item -Path $legacyPath -Name InprocServer32 -Value '' -Force -ErrorAction Stop | Out-Null
if ($null -eq $workaroundKey) {
# The first override ensures the CLSID parent exists, so -Force is unnecessary.
$workaroundKey = New-Item -Path $workaroundPath -ErrorAction Stop
try {
New-ItemProperty -LiteralPath $workaroundPath -Name $markerName -Value 1 -PropertyType DWord -ErrorAction Stop | Out-Null
} catch {
if ($workaroundKey.ValueCount -eq 0 -and $workaroundKey.SubKeyCount -eq 0) {
Remove-Item -LiteralPath $workaroundPath -ErrorAction Stop
}
throw
}
$ownsWorkaround = $true
}
if ($ownsWorkaround -and -not (Test-Path -LiteralPath $serverPath -ErrorAction Stop)) {
New-Item -Path $serverPath -Value '' -ErrorAction Stop | Out-Null
} elseif ($ownsWorkaround) {
$serverKey = Get-Item -LiteralPath $serverPath -ErrorAction Stop
if ($serverKey.ValueCount -eq 0 -and $serverKey.SubKeyCount -eq 0) {
# Key creation can succeed before assigning its default value fails.
Set-Item -LiteralPath $serverPath -Value '' -Force -ErrorAction Stop
}
}
}
$sessionId = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
Get-Process -Name explorer -ErrorAction SilentlyContinue |
Where-Object { $_.SessionId -eq $sessionId } |
ForEach-Object { Stop-Process -Id $_.Id -ErrorAction SilentlyContinue }
}
+230
View File
@@ -0,0 +1,230 @@
BeforeAll {
$repoRoot = Split-Path $PSScriptRoot -Parent
. (Join-Path $repoRoot 'functions/private/Set-WinUtilClassicContextMenu.ps1')
. (Join-Path $repoRoot 'functions/private/Invoke-WinUtilScript.ps1')
. (Join-Path $repoRoot 'functions/private/Invoke-WinUtilTweaks.ps1')
$script:classicMenuTweaks = Get-Content (Join-Path $repoRoot 'config/tweaks.json') -Raw | ConvertFrom-Json
$script:classicMenuTweakName = 'WPFTweaksRightClickMenu'
$script:legacyMenuPath = 'HKCU:\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}'
$script:workaroundMenuPath = 'HKCU:\Software\Classes\CLSID\{51668040-5e31-4023-bbe3-149163bd80f1}'
$script:workaroundServerPath = "$script:workaroundMenuPath\InprocServer32"
[xml]$unattend = Get-Content (Join-Path $repoRoot 'tools/autounattend.xml') -Raw
$userOnce = $unattend.SelectSingleNode("//*[local-name()='File' and @path='C:\Windows\Setup\Scripts\UserOnce.ps1']").InnerText
$ast = [System.Management.Automation.Language.Parser]::ParseInput($userOnce, [ref]$null, [ref]$null)
$blocks = @($ast.FindAll({
param($node)
$node -is [System.Management.Automation.Language.ScriptBlockExpressionAst] -and
$node.ScriptBlock.Extent.Text -match '^\{\s*reg\.exe add "HKCU\\Software\\Classes\\CLSID\\\{86ca1aa0'
}, $true))
if ($blocks.Count -ne 1) { throw 'Expected one unattended classic menu block.' }
$script:unattendedClassicMenu = $blocks[0].ScriptBlock.GetScriptBlock()
function reg.exe { param([Parameter(ValueFromRemainingArguments)][string[]]$Arguments) }
function Write-WinUtilLog {
param($Level, $Component, $Message)
if ($Level -eq 'ERROR') {
$global:WinUtilJobErrorCount++
}
}
function New-TestRegistryKey {
param([hashtable]$Values = @{}, [string[]]$SubKeys = @())
$key = [pscustomobject]@{ Values = $Values; SubKeys = $SubKeys }
$key | Add-Member ScriptProperty ValueCount { $this.Values.Count }
$key | Add-Member ScriptProperty SubKeyCount { $this.SubKeys.Count }
$key | Add-Member ScriptMethod GetValue { param($Name) $this.Values[$Name] }
$key | Add-Member ScriptMethod GetValueNames { @($this.Values.Keys) }
$key | Add-Member ScriptMethod GetSubKeyNames { $this.SubKeys }
$key | Add-Member ScriptMethod GetValueKind { [Microsoft.Win32.RegistryValueKind]::String }
$key
}
}
Describe 'Classic context menu apply and undo' {
BeforeEach {
$script:sync = @{ configs = @{ tweaks = $script:classicMenuTweaks } }
$script:previousClassicMenuErrorCount = $global:WinUtilJobErrorCount
$global:WinUtilJobErrorCount = 0
$script:menuRegistry = @{}
Mock Write-Warning {}
Mock Stop-Process {}
Mock Get-Process { [pscustomobject]@{ Id = 12345; SessionId = [System.Diagnostics.Process]::GetCurrentProcess().SessionId } }
Mock reg.exe {}
Mock Get-Item {
$keyPath = [string]$LiteralPath
if (-not $script:menuRegistry.ContainsKey($keyPath)) {
throw [System.Management.Automation.ItemNotFoundException]::new($keyPath)
}
$script:menuRegistry[$keyPath]
}
Mock Test-Path { $script:menuRegistry.ContainsKey([string]$LiteralPath) }
Mock New-Item {
$keyPath = [string]$Path
if ($keyPath -eq $script:legacyMenuPath) {
$script:menuRegistry[$keyPath] = New-TestRegistryKey -SubKeys @('InprocServer32')
} elseif ($keyPath -eq $script:workaroundServerPath) {
$script:menuRegistry[$keyPath] = New-TestRegistryKey -Values @{ '' = $Value }
$script:menuRegistry[$script:workaroundMenuPath].SubKeys = @('InprocServer32')
} else {
$script:menuRegistry[$keyPath] = New-TestRegistryKey
}
$script:menuRegistry[$keyPath]
}
Mock New-ItemProperty { $script:menuRegistry[[string]$LiteralPath].Values[$Name] = $Value }
Mock Set-Item { $script:menuRegistry[[string]$LiteralPath].Values[''] = $Value }
Mock Remove-Item {
$keyPath = [string]$LiteralPath
if (-not $script:menuRegistry.ContainsKey($keyPath)) {
throw [System.Management.Automation.ItemNotFoundException]::new($keyPath)
}
$script:menuRegistry.Remove($keyPath)
if ($keyPath -eq $script:workaroundMenuPath) {
$script:menuRegistry.Remove($script:workaroundServerPath)
}
}
}
AfterEach {
$global:WinUtilJobErrorCount = $script:previousClassicMenuErrorCount
Remove-Variable sync -Scope Script -ErrorAction SilentlyContinue
}
It 'creates the newer unattended registration and preserves it on a repeated run' {
& $script:unattendedClassicMenu
& $script:unattendedClassicMenu
$script:menuRegistry[$script:workaroundServerPath].Values[''] | Should -BeExactly ''
$script:menuRegistry[$script:workaroundMenuPath].Values['WinUtilClassicContextMenu'] | Should -Be 1
Should -Invoke New-ItemProperty -Times 1 -Exactly
}
It 'preserves a pre-existing unattended registration' {
$script:menuRegistry[$script:workaroundMenuPath] = New-TestRegistryKey -Values @{ Vendor = 'Custom' }
& $script:unattendedClassicMenu
$script:menuRegistry[$script:workaroundMenuPath].Values['Vendor'] | Should -Be 'Custom'
Should -Invoke New-ItemProperty -Times 0 -Exactly
}
It 'reports an unattended marker failure without terminating user setup' {
Mock New-ItemProperty {
if (-not $script:allowMenuMarkerWrite) { throw 'Marker write denied' }
$script:menuRegistry[[string]$LiteralPath].Values[$Name] = $Value
}
$script:allowMenuMarkerWrite = $false
{ & $script:unattendedClassicMenu } | Should -Not -Throw
Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -like '*Marker write denied*' }
$script:menuRegistry.ContainsKey($script:workaroundMenuPath) | Should -BeFalse
$script:allowMenuMarkerWrite = $true
& $script:unattendedClassicMenu
$script:menuRegistry[$script:workaroundServerPath].Values[''] | Should -BeExactly ''
}
It 'creates the second registration once and removes both overrides on undo' {
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$script:menuRegistry[$script:workaroundServerPath].Values[''] | Should -BeExactly ''
$script:menuRegistry[$script:workaroundMenuPath].Values['WinUtilClassicContextMenu'] | Should -Be 1
Should -Invoke New-Item -Times 1 -Exactly -ParameterFilter { [string]$Path -eq $script:workaroundServerPath -and -not $Force }
Should -Invoke New-ItemProperty -Times 1 -Exactly
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName -undo $true
$script:menuRegistry.Count | Should -Be 0
$global:WinUtilJobErrorCount | Should -Be 0
Should -Invoke Stop-Process -Times 3 -Exactly -ParameterFilter { $Id -eq 12345 }
}
It 'leaves Explorer in other sessions running on apply and undo' {
Mock Get-Process {
$currentSession = [System.Diagnostics.Process]::GetCurrentProcess().SessionId
[pscustomobject]@{ Id = 12345; SessionId = $currentSession }
[pscustomobject]@{ Id = 23456; SessionId = $currentSession + 1 }
}
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName -undo $true
Should -Invoke Stop-Process -Times 2 -Exactly -ParameterFilter { $Id -eq 12345 }
Should -Invoke Stop-Process -Times 0 -Exactly -ParameterFilter { $Id -eq 23456 -or $Name }
}
It 'preserves a pre-existing second registration across apply and undo' {
$script:menuRegistry[$script:workaroundMenuPath] = New-TestRegistryKey -Values @{ Vendor = 'Custom' } -SubKeys @('InprocServer32', 'Other')
$script:menuRegistry[$script:workaroundServerPath] = New-TestRegistryKey -Values @{ '' = 'custom.dll' }
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName -undo $true
$script:menuRegistry[$script:workaroundMenuPath].Values['Vendor'] | Should -Be 'Custom'
$script:menuRegistry[$script:workaroundServerPath].Values[''] | Should -Be 'custom.dll'
Should -Invoke New-ItemProperty -Times 0 -Exactly
Should -Invoke Remove-Item -Times 0 -Exactly -ParameterFilter { [string]$LiteralPath -eq $script:workaroundMenuPath }
}
It 'leaves later changes to a WinUtil-owned registration intact' {
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$script:menuRegistry[$script:workaroundServerPath].Values[''] = 'custom.dll'
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName -undo $true
$script:menuRegistry[$script:workaroundServerPath].Values[''] | Should -Be 'custom.dll'
$global:WinUtilJobErrorCount | Should -Be 1
Should -Invoke Stop-Process -Times 1 -Exactly
}
It 'accepts already absent keys on undo' {
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName -undo $true
$global:WinUtilJobErrorCount | Should -Be 0
Should -Invoke Stop-Process -Times 1 -Exactly
}
It 'reports a denied deletion and skips the Explorer restart for the <Registration> override' -ForEach @(
@{ Registration = 'original' }
@{ Registration = 'newer' }
) {
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$script:deniedMenuPath = if ($Registration -eq 'original') { $script:legacyMenuPath } else { $script:workaroundMenuPath }
Mock Remove-Item { throw [System.UnauthorizedAccessException]::new('Registry deletion denied') } -ParameterFilter {
[string]$LiteralPath -eq $script:deniedMenuPath -and $ErrorAction -eq 'Stop'
}
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName -undo $true
$script:menuRegistry.ContainsKey($script:deniedMenuPath) | Should -BeTrue
$global:WinUtilJobErrorCount | Should -Be 1
Should -Invoke Stop-Process -Times 1 -Exactly
}
It 'completes an empty owned server key after a partial creation failure' {
Mock New-Item {
$script:menuRegistry[$script:workaroundServerPath] = New-TestRegistryKey
$script:menuRegistry[$script:workaroundMenuPath].SubKeys = @('InprocServer32')
throw [System.UnauthorizedAccessException]::new('Default value creation denied')
} -ParameterFilter { [string]$Path -eq $script:workaroundServerPath }
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$global:WinUtilJobErrorCount | Should -Be 1
$script:menuRegistry[$script:workaroundServerPath].ValueCount | Should -Be 0
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$script:menuRegistry[$script:workaroundServerPath].Values.ContainsKey('') | Should -BeTrue
$script:menuRegistry[$script:workaroundServerPath].Values[''] | Should -BeExactly ''
Should -Invoke Stop-Process -Times 1 -Exactly
}
It 'retries a failed server creation without overwriting the owned root' {
Mock New-Item { throw [System.UnauthorizedAccessException]::new('Registry creation denied') } -ParameterFilter {
[string]$Path -eq $script:workaroundServerPath -and -not $script:menuCreationAllowed
}
$script:menuCreationAllowed = $false
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$global:WinUtilJobErrorCount | Should -Be 1
$script:menuRegistry.ContainsKey($script:workaroundServerPath) | Should -BeFalse
$script:menuCreationAllowed = $true
Invoke-WinUtilTweaks -CheckBox $script:classicMenuTweakName
$script:menuRegistry.ContainsKey($script:workaroundServerPath) | Should -BeTrue
$global:WinUtilJobErrorCount | Should -Be 1
Should -Invoke New-ItemProperty -Times 1 -Exactly
Should -Invoke Stop-Process -Times 1 -Exactly
}
}
+30
View File
@@ -323,6 +323,36 @@ $scripts = @(
};
{
reg.exe add "HKCU\Software\Classes\CLSID\{86ca1aa0-34aa-4e8b-a509-50c905bae2a2}\InprocServer32" /ve /f;
$createdClassicMenuKey = $null;
try {
$classicMenuPath = 'HKCU:\Software\Classes\CLSID\{51668040-5e31-4023-bbe3-149163bd80f1}';
if (-not (Test-Path -LiteralPath $classicMenuPath -ErrorAction Stop)) {
$createdClassicMenuKey = New-Item -Path $classicMenuPath -ErrorAction Stop;
New-ItemProperty -LiteralPath $classicMenuPath -Name 'WinUtilClassicContextMenu' -Value 1 -PropertyType DWord -ErrorAction Stop | Out-Null;
};
$classicMenuKey = Get-Item -LiteralPath $classicMenuPath -ErrorAction Stop;
$classicMenuMarker = $classicMenuKey.GetValue('WinUtilClassicContextMenu');
if ($classicMenuMarker -is [int] -and $classicMenuMarker -eq 1) {
$classicMenuServer = "$classicMenuPath\InprocServer32";
if (-not (Test-Path -LiteralPath $classicMenuServer -ErrorAction Stop)) {
New-Item -Path $classicMenuServer -Value '' -ErrorAction Stop | Out-Null;
} else {
$classicMenuServerKey = Get-Item -LiteralPath $classicMenuServer -ErrorAction Stop;
if ($classicMenuServerKey.ValueCount -eq 0 -and $classicMenuServerKey.SubKeyCount -eq 0) {
Set-Item -LiteralPath $classicMenuServer -Value '' -Force -ErrorAction Stop;
};
};
};
} catch {
Write-Warning "Could not apply the newer classic context menu workaround: $($_.Exception.Message)";
if ($null -ne $createdClassicMenuKey -and $createdClassicMenuKey.ValueCount -eq 0 -and $createdClassicMenuKey.SubKeyCount -eq 0) {
try {
Remove-Item -LiteralPath $classicMenuPath -ErrorAction Stop;
} catch {
Write-Warning "Could not remove the empty classic context menu key: $($_.Exception.Message)";
};
};
};
};
{
Set-ItemProperty -LiteralPath 'Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name 'LaunchTo' -Type 'DWord' -Value 1;