$ErrorActionPreference = 'Stop' $target = [IO.Path]::GetFullPath($PSScriptRoot).TrimEnd('\') if (!(Test-Path -LiteralPath (Join-Path $target '.musicbridge-install'))) { throw 'Installation marker is missing; refusing deletion.' } if ($target -eq [IO.Path]::GetPathRoot($target)) { throw 'Unsafe directory.' } $exe = Join-Path $target 'MusicBridge.Agent.exe' if (Test-Path -LiteralPath $exe) { try { $probe = [IO.File]::Open($exe, 'Open', 'ReadWrite', 'None'); $probe.Dispose() } catch { throw 'Close MusicBridge from its tray menu first.' } } $run = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run' $command = (Get-ItemProperty -LiteralPath $run -Name 'MusicBridge.Agent' -ErrorAction SilentlyContinue).'MusicBridge.Agent' if ($command -and $command.StartsWith('"' + $exe + '"', [StringComparison]::OrdinalIgnoreCase)) { Remove-ItemProperty -LiteralPath $run -Name 'MusicBridge.Agent' } $link = Join-Path ([Environment]::GetFolderPath('Programs')) 'MusicBridge.lnk' if (Test-Path -LiteralPath $link) { $shell = New-Object -ComObject WScript.Shell if ($shell.CreateShortcut($link).TargetPath -eq $exe) { Remove-Item -LiteralPath $link } } # Delete only the verified installed directory, preserving %LOCALAPPDATA%\MusicBridge identity. Remove-Item -LiteralPath $target -Recurse -Force Write-Host 'MusicBridge removed. Pairing identity was preserved.'