Files
WindowsVolumeSteps/Uninstall-VolumeSteps.ps1
T
2026-10-08 12:31:29 +00:00

66 lines
5.0 KiB
PowerShell

# SPDX-License-Identifier: GPL-3.0-only
param([switch]$KeepFiles)
$ErrorActionPreference = 'Stop'
$volumeInstall = [IO.Path]::GetFullPath($PSScriptRoot).TrimEnd('\')
$volumeMarkerPath = Join-Path $volumeInstall 'installation.json'
$volumeMarker = Get-Content -LiteralPath $volumeMarkerPath -Raw | ConvertFrom-Json
if ($volumeMarker.owner -ne 'WindowsVolumeSteps' -or $volumeMarker.installPath -ne $volumeInstall -or !$volumeMarker.files) { throw 'Installation ownership or file manifest validation failed.' }
# Resolve and validate every owned file before removing anything.
$volumeFiles = @($volumeMarker.files | ForEach-Object {
$volumeAbsolute = [IO.Path]::GetFullPath((Join-Path $volumeInstall $_))
if (!$volumeAbsolute.StartsWith($volumeInstall + '\', [StringComparison]::OrdinalIgnoreCase)) { throw 'Invalid owned-file path.' }
$volumeAbsolute
})
& (Join-Path $volumeInstall 'Disable-VolumeSteps.ps1')
for ($volumeAttempt = 0; $volumeAttempt -lt 50; $volumeAttempt++) {
if (!@(Get-Process windhawk -ErrorAction SilentlyContinue | Where-Object Path -EQ (Join-Path $volumeInstall 'windhawk.exe')).Count) { break }
Start-Sleep -Milliseconds 100
}
if (@(Get-Process windhawk -ErrorAction SilentlyContinue | Where-Object Path -EQ (Join-Path $volumeInstall 'windhawk.exe')).Count) { throw 'The helper is still running; its files were preserved.' }
$volumeCleanup = Join-Path ([Environment]::GetFolderPath('Startup')) 'Windows Volume Steps - Finish Uninstall.lnk'
$volumeCleanupArguments = '-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File "' + (Join-Path $volumeInstall 'Uninstall-VolumeSteps.ps1') + '"'
$volumeShell = New-Object -ComObject WScript.Shell
if ((Test-Path -LiteralPath $volumeCleanup) -and $volumeShell.CreateShortcut($volumeCleanup).Arguments -ne $volumeCleanupArguments) { throw 'The cleanup shortcut belongs to another installation; it was preserved.' }
if (!$KeepFiles) {
$volumeControlNames = @('installation.json','payload-manifest.json','Uninstall-VolumeSteps.ps1','Disable-VolumeSteps.ps1','Uninstall-VolumeSteps.cmd','Disable-VolumeSteps.cmd','AppData\Engine\Mods\windows-11-settings-styler.ini','AppData\Engine\Mods\windows-11-notification-center-styler.ini')
$volumeControls = @($volumeControlNames | ForEach-Object { Join-Path $volumeInstall $_ })
$volumePending = @()
# Shared runtime DLLs can remain mapped by a Windows panel. Preserve the
# cleanup controls until all runtime files can be removed after sign-in.
foreach ($volumeFile in @($volumeFiles | Where-Object { $_ -notin $volumeControls })) {
if (Test-Path -LiteralPath $volumeFile -PathType Leaf) {
try { Remove-Item -LiteralPath $volumeFile -Force }
catch { $volumePending += $volumeFile }
}
}
if ($volumePending.Count) {
$volumeShortcut = $volumeShell.CreateShortcut($volumeCleanup)
$volumeShortcut.TargetPath = Join-Path $env:WINDIR 'System32\WindowsPowerShell\v1.0\powershell.exe'
$volumeShortcut.Arguments = $volumeCleanupArguments
$volumeShortcut.WorkingDirectory = $volumeInstall
$volumeShortcut.WindowStyle = 7
$volumeShortcut.Description = 'Finish removing Windows Volume Steps after native panels release the runtime'
$volumeShortcut.Save()
Write-Output 'Volume steps are disabled. Windows is using some runtime files; their removal will finish at your next sign-in.'
return
}
foreach ($volumeFile in $volumeControls) { if (Test-Path -LiteralPath $volumeFile -PathType Leaf) { Remove-Item -LiteralPath $volumeFile -Force } }
$volumeOwnedDirectories = @($volumeFiles | ForEach-Object {
$volumeParent = Split-Path $_ -Parent
while ($volumeParent -ne $volumeInstall -and $volumeParent.StartsWith($volumeInstall + '\', [StringComparison]::OrdinalIgnoreCase)) {
$volumeParent
$volumeParent = Split-Path $volumeParent -Parent
}
}) + @('AppData\Engine\ModsWritable\mod-status','AppData\Engine\ModsWritable\mod-task','AppData\Engine\ModsWritable','AppData\Engine\Symbols') | ForEach-Object {
if ([IO.Path]::IsPathRooted($_)) { $_ } else { Join-Path $volumeInstall $_ }
} | Sort-Object -Unique | Sort-Object Length -Descending
foreach ($volumeDirectory in $volumeOwnedDirectories) {
if ((Test-Path -LiteralPath $volumeDirectory -PathType Container) -and !@(Get-ChildItem -LiteralPath $volumeDirectory -Force).Count) { Remove-Item -LiteralPath $volumeDirectory }
}
if ((Test-Path -LiteralPath $volumeInstall) -and !@(Get-ChildItem -LiteralPath $volumeInstall -Force).Count) { Remove-Item -LiteralPath $volumeInstall }
}
if (Test-Path -LiteralPath $volumeCleanup) { Remove-Item -LiteralPath $volumeCleanup }
$volumeRegistry = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\WindowsVolumeSteps'
if ((Test-Path -LiteralPath $volumeRegistry) -and (Get-ItemProperty -LiteralPath $volumeRegistry).InstallLocation -eq $volumeInstall) { Remove-Item -LiteralPath $volumeRegistry }
Write-Output 'Windows Volume Steps uninstalled. Native volume behavior restored.'