Files
douyi_guaji_main/tools/fix_start_menu_win11.ps1
T
Your Name dcded08796 feat: enhance swipe and back navigation logic
- Adjusted configuration for reset after swipes to 10 seconds and added action to go back.
- Updated flow control to skip back action if a recent reset was triggered within 5 seconds.
- Implemented a mechanism to enable back navigation when consecutive misses reach a threshold.
- Added GUI options to enable or disable back navigation on misses, ensuring consistency with user settings.
- Introduced new diagnostic tools for runtime flag checks and screenshot diagnostics.
- Created PowerShell scripts for restoring Start Menu shortcuts and performing system health checks.
- Added tests for swipe operations and back navigation to validate new logic.
2026-01-06 16:55:00 +08:00

227 lines
7.8 KiB
PowerShell

[CmdletBinding()]
param(
[ValidateSet('Auto','Diagnose','FixShortcuts','RestartShell','ReregisterAppx')]
[string]$Mode = 'Auto'
)
$ErrorActionPreference = 'Stop'
function Write-Section([string]$title) {
Write-Host ""
Write-Host "=== $title ==="
}
function Test-IsAdmin {
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function Get-UserSid {
return ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
}
function Get-ProfileImagePathFromRegistry([string]$sid) {
$key = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$sid"
if (Test-Path $key) {
return (Get-ItemProperty $key).ProfileImagePath
}
return $null
}
function Get-LnkCount([string]$path) {
if (!(Test-Path $path)) { return -1 }
return (Get-ChildItem -LiteralPath $path -Recurse -Filter '*.lnk' -ErrorAction SilentlyContinue | Measure-Object).Count
}
function Ensure-Directory([string]$path) {
if (!(Test-Path $path)) {
New-Item -ItemType Directory -Path $path -Force | Out-Null
}
}
function Get-StartMenuPaths {
$programsUser = Join-Path $env:APPDATA 'Microsoft\Windows\Start Menu\Programs'
$programsCommon = Join-Path $env:ProgramData 'Microsoft\Windows\Start Menu\Programs'
return [pscustomobject]@{
ProgramsUser = $programsUser
ProgramsCommon = $programsCommon
}
}
function Find-CandidateProgramsUserPaths([string]$currentProgramsUser) {
$candidates = @()
$user = [Environment]::UserName
$roots = @('C:\Users', 'D:\Users', 'E:\Users', 'C:\Windows.old\Users')
foreach ($root in $roots) {
if (!(Test-Path $root)) { continue }
Get-ChildItem -LiteralPath $root -Directory -ErrorAction SilentlyContinue | ForEach-Object {
$candidate = Join-Path $_.FullName 'AppData\Roaming\Microsoft\Windows\Start Menu\Programs'
if ($candidate -ine $currentProgramsUser -and (Test-Path -LiteralPath $candidate -ErrorAction SilentlyContinue)) {
$count = Get-LnkCount $candidate
$candidates += [pscustomobject]@{ Path = $candidate; LnkCount = $count }
}
}
}
$candidates | Sort-Object LnkCount -Descending | Select-Object -First 10
}
function Backup-Folder([string]$sourcePath, [string]$backupRoot) {
if (!(Test-Path $sourcePath)) { return $null }
Ensure-Directory $backupRoot
$leaf = Split-Path $sourcePath -Leaf
$backupPath = Join-Path $backupRoot $leaf
Ensure-Directory $backupPath
Copy-Item -LiteralPath $sourcePath -Destination $backupPath -Recurse -Force -ErrorAction SilentlyContinue
return $backupPath
}
function Copy-Shortcuts([string]$fromPath, [string]$toPath) {
Ensure-Directory $toPath
$items = Get-ChildItem -LiteralPath $fromPath -Force -ErrorAction SilentlyContinue
foreach ($item in $items) {
if ($item.Name -ieq 'desktop.ini') { continue }
Copy-Item -LiteralPath $item.FullName -Destination $toPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
function Restart-Shell {
Write-Section 'Restart Shell/Start Menu'
$procs = @('StartMenuExperienceHost','ShellExperienceHost')
foreach ($p in $procs) {
Get-Process -Name $p -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
}
Get-Process -Name explorer -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
Start-Sleep -Milliseconds 600
Start-Process explorer.exe
Write-Host 'Explorer restarted.'
}
function Reregister-AppxCurrentUser {
Write-Section 'Re-register AppX (Current User)'
Get-AppxPackage | ForEach-Object {
$manifest = Join-Path $_.InstallLocation 'AppxManifest.xml'
if (Test-Path $manifest) {
try {
Add-AppxPackage -DisableDevelopmentMode -Register $manifest -ErrorAction Stop | Out-Null
} catch {
# Keep going; many packages will throw benign errors
}
}
}
Write-Host 'AppX re-register done (current user).'
}
function Diagnose {
$sid = Get-UserSid
$profileImagePath = Get-ProfileImagePathFromRegistry $sid
$paths = Get-StartMenuPaths
Write-Section 'Diagnose'
Write-Host "Admin: $(Test-IsAdmin)"
Write-Host "User: $([Environment]::UserName)"
Write-Host "SID: $sid"
Write-Host "ProfileImagePath(HKLM): $profileImagePath"
Write-Host "USERPROFILE: $env:USERPROFILE"
Write-Host "APPDATA: $env:APPDATA"
Write-Host "ProgramData: $env:ProgramData"
Write-Host "ProgramsUser: $($paths.ProgramsUser)"
Write-Host "ProgramsCommon: $($paths.ProgramsCommon)"
$userExists = Test-Path $paths.ProgramsUser
$commonExists = Test-Path $paths.ProgramsCommon
Write-Host "User.Exists=$userExists LnkCount=$(Get-LnkCount $paths.ProgramsUser)"
Write-Host "Common.Exists=$commonExists LnkCount=$(Get-LnkCount $paths.ProgramsCommon)"
Write-Section 'Top-level items (first 20)'
if ($userExists) {
Write-Host "[User] $($paths.ProgramsUser)"
Get-ChildItem -LiteralPath $paths.ProgramsUser -Force -ErrorAction SilentlyContinue | Select-Object -First 20 Name | ForEach-Object { $_.Name }
} else {
Write-Host "[User] (missing)"
}
if ($commonExists) {
Write-Host "[Common] $($paths.ProgramsCommon)"
Get-ChildItem -LiteralPath $paths.ProgramsCommon -Force -ErrorAction SilentlyContinue | Select-Object -First 20 Name | ForEach-Object { $_.Name }
} else {
Write-Host "[Common] (missing)"
}
return [pscustomobject]@{
Sid = $sid
ProfileImagePath = $profileImagePath
ProgramsUser = $paths.ProgramsUser
ProgramsCommon = $paths.ProgramsCommon
UserLnkCount = Get-LnkCount $paths.ProgramsUser
CommonLnkCount = Get-LnkCount $paths.ProgramsCommon
}
}
function Fix-Shortcuts {
Write-Section 'Fix Start Menu Shortcuts'
$diag = Diagnose
Ensure-Directory $diag.ProgramsUser
Ensure-Directory $diag.ProgramsCommon
$now = Get-Date -Format 'yyyyMMdd_HHmmss'
$desktop = [Environment]::GetFolderPath('Desktop')
$backupRoot = Join-Path $desktop "StartMenuBackup_$now"
Write-Host "BackupRoot: $backupRoot"
$backupUser = Backup-Folder $diag.ProgramsUser $backupRoot
$backupCommon = Backup-Folder $diag.ProgramsCommon $backupRoot
Write-Host "BackupUser: $backupUser"
Write-Host "BackupCommon: $backupCommon"
$candidates = Find-CandidateProgramsUserPaths $diag.ProgramsUser
if (!$candidates -or $candidates.Count -eq 0) {
Write-Host 'No candidate old Programs paths found under C:\Users/D:\Users/E:\Users.'
return
}
Write-Section 'Candidate old ProgramsUser paths'
$candidates | Select-Object -First 5 | Format-Table -AutoSize
$best = $candidates | Select-Object -First 1
if ($best.LnkCount -le $diag.UserLnkCount) {
Write-Host "Current ProgramsUser already has >= shortcuts than best candidate. (Current=$($diag.UserLnkCount), Best=$($best.LnkCount))"
return
}
Write-Host "Copying shortcuts from: $($best.Path)"
Write-Host "To (current user programs): $($diag.ProgramsUser)"
Copy-Shortcuts $best.Path $diag.ProgramsUser
Write-Host "AfterCopy User LnkCount=$(Get-LnkCount $diag.ProgramsUser)"
}
switch ($Mode) {
'Diagnose' { Diagnose | Out-Null; break }
'FixShortcuts' { Fix-Shortcuts; break }
'RestartShell' { Restart-Shell; break }
'ReregisterAppx' { Reregister-AppxCurrentUser; break }
'Auto' {
$diag = Diagnose
Fix-Shortcuts
Restart-Shell
# If Start Menu is still missing many Store apps, user can run this explicitly.
Write-Host "\nTip: If Store apps are still missing, run: .\\tools\\fix_start_menu_win11.ps1 -Mode ReregisterAppx"
break
}
}