[CmdletBinding()] param( [switch]$SkipDism, [switch]$SkipSfc, [switch]$SkipAppx ) $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) } $now = Get-Date -Format 'yyyyMMdd_HHmmss' $logPath = Join-Path ([Environment]::GetFolderPath('Desktop')) "StartMenuFix_Admin_$now.log" "StartMenuFix Admin started: $(Get-Date)" | Out-File -FilePath $logPath -Encoding UTF8 "Running as admin: $(Test-IsAdmin)" | Out-File -FilePath $logPath -Append -Encoding UTF8 if (!(Test-IsAdmin)) { "ERROR: This script must be run as Administrator." | Out-File -FilePath $logPath -Append -Encoding UTF8 throw 'This script must be run as Administrator.' } Write-Host "Log: $logPath" try { if (-not $SkipDism) { Write-Section 'DISM RestoreHealth' "DISM start: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 DISM /Online /Cleanup-Image /RestoreHealth | Out-File -FilePath $logPath -Append -Encoding UTF8 "DISM end: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 } if (-not $SkipSfc) { Write-Section 'SFC Scannow' "SFC start: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 sfc /scannow | Out-File -FilePath $logPath -Append -Encoding UTF8 "SFC end: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 } if (-not $SkipAppx) { Write-Section 'Re-register AppX (All Users)' "AppX re-register start: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 Get-AppxPackage -AllUsers | 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; capture error into log for diagnostics "WARN: $($_.Name) - $($_.Exception.Message)" | Out-File -FilePath $logPath -Append -Encoding UTF8 } } } "AppX re-register end: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 } Write-Section 'Restart Explorer' "Restart explorer: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 Get-Process -Name StartMenuExperienceHost -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue Get-Process -Name ShellExperienceHost -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 "DONE: $(Get-Date)" | Out-File -FilePath $logPath -Append -Encoding UTF8 Write-Host 'Done. Explorer restarted.' Write-Host "Log saved: $logPath" } catch { "FATAL: $($_.Exception.Message)" | Out-File -FilePath $logPath -Append -Encoding UTF8 throw }