# Code Sandbox Windows 彻底卸载脚本,可重复运行 $ErrorActionPreference = 'Continue' $report = New-Object System.Collections.Generic.List[string] function Note([string]$msg) { $script:report.Add($msg); Write-Host $msg } $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) # 0. 备份授权身份文件到桌面 $backup = Join-Path ([Environment]::GetFolderPath('Desktop')) ("CodeSandbox-backup-" + (Get-Date -Format 'yyyyMMdd-HHmmss')) $identityCandidates = @( (Join-Path $env:LOCALAPPDATA 'CodeSandbox\license-state.json'), (Join-Path $env:LOCALAPPDATA 'CodeSandbox\licensing'), (Join-Path $env:LOCALAPPDATA 'CodeSandbox\install.json'), (Join-Path $env:APPDATA 'code-sandbox\license-state.json'), (Join-Path $env:APPDATA 'code-sandbox\licensing') ) $found = $identityCandidates | Where-Object { Test-Path -LiteralPath $_ } if ($found) { New-Item -ItemType Directory -Path $backup -Force | Out-Null foreach ($item in $found) { Copy-Item -LiteralPath $item -Destination $backup -Recurse -Force } Note ("[备份] 授权身份文件已备份到 " + $backup) } else { Note '[备份] 没有找到授权身份文件,跳过' } # 1. 结束正在运行的 App 进程 Get-Process -Name 'Code Sandbox' -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue Note ("[进程] 已结束 Code Sandbox 进程 PID " + $_.Id) } # 2. 跑自带卸载器 $uninstallString = $null $regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\CodeSandbox' if (Test-Path $regPath) { $uninstallString = (Get-ItemProperty -Path $regPath -Name UninstallString -ErrorAction SilentlyContinue).UninstallString } $builtInRun = $false if ($uninstallString) { $exe = $uninstallString.Trim('"') if (Test-Path -LiteralPath $exe) { Start-Process -FilePath $exe -Wait -ErrorAction SilentlyContinue $builtInRun = $true Start-Sleep -Seconds 15 Note '[自带卸载] 已运行 Uninstall Code Sandbox.exe' } } if (-not $builtInRun) { $launcher = Join-Path $env:LOCALAPPDATA 'CodeSandbox\Code Sandbox.exe' if (Test-Path -LiteralPath $launcher) { Start-Process -FilePath $launcher -ArgumentList '--uninstall' -ErrorAction SilentlyContinue Start-Sleep -Seconds 15 Note '[自带卸载] 已运行 Code Sandbox.exe --uninstall' } else { Note '[自带卸载] 没有找到卸载器,可能已经被卸过,继续清理残留' } } # 3. 注销 code-sandbox-* WSL 发行版 $wsl = Join-Path $env:SystemRoot 'System32\wsl.exe' if (Test-Path $wsl) { $distros = (& $wsl -l -q 2>$null) -replace "`0", '' | ForEach-Object { $_.Trim() } | Where-Object { $_ -like 'code-sandbox-*' } if ($distros) { foreach ($d in $distros) { & $wsl --terminate $d 2>$null | Out-Null & $wsl --unregister $d 2>$null | Out-Null if ($LASTEXITCODE -eq 0) { Note ("[WSL] 已注销发行版 " + $d) } else { Note ("[WSL] 注销失败,待手动处理 " + $d) } } } else { Note '[WSL] 没有 code-sandbox- 开头的发行版' } } else { Note '[WSL] 系统没有 wsl.exe,跳过' } # 4. 系统层清理(需要管理员) if ($isAdmin) { if (Get-Service -Name 'CodeSandboxPrivHelper' -ErrorAction SilentlyContinue) { Stop-Service -Name 'CodeSandboxPrivHelper' -Force -ErrorAction SilentlyContinue & sc.exe delete CodeSandboxPrivHelper | Out-Null Note '[服务] 已删除 CodeSandboxPrivHelper' } else { Note '[服务] CodeSandboxPrivHelper 不存在' } $rules = Get-NetFirewallRule -ErrorAction SilentlyContinue | Where-Object { $_.Name -like 'CodeSandbox-*' -or $_.DisplayName -like 'CodeSandbox-*' -or $_.DisplayGroup -like 'CodeSandbox*' } if ($rules) { $rules | Remove-NetFirewallRule -ErrorAction SilentlyContinue Note ("[防火墙] 已删除规则 " + $rules.Count + " 条") } else { Note '[防火墙] 没有 CodeSandbox 相关规则' } if (Test-Path -LiteralPath "$env:ProgramData\CodeSandbox") { Remove-Item -LiteralPath "$env:ProgramData\CodeSandbox" -Recurse -Force -ErrorAction SilentlyContinue if (Test-Path -LiteralPath "$env:ProgramData\CodeSandbox") { Note '[系统目录] %ProgramData%\CodeSandbox 删除失败,待手动处理' } else { Note '[系统目录] 已删除 %ProgramData%\CodeSandbox' } } } else { Note '[权限] 当前不是管理员,服务、防火墙规则和 %ProgramData%\CodeSandbox 留到最后补做' } # 5. 删除用户数据目录 $dataRoots = @( (Join-Path $env:LOCALAPPDATA 'CodeSandbox'), (Join-Path $env:APPDATA 'code-sandbox'), (Join-Path $env:LOCALAPPDATA 'code-sandbox') ) if ($env:CODE_SANDBOX_DATA_DIR) { $dataRoots += (Join-Path $env:CODE_SANDBOX_DATA_DIR 'code-sandbox') } foreach ($root in $dataRoots) { if (Test-Path -LiteralPath $root) { Remove-Item -LiteralPath $root -Recurse -Force -ErrorAction SilentlyContinue if (Test-Path -LiteralPath $root) { Note ("[数据] 删除失败,可能有文件被占用 " + $root) } else { Note ("[数据] 已删除 " + $root) } } else { Note ("[数据] 不存在,跳过 " + $root) } } # 6. 收尾检查 if (Test-Path $regPath) { Remove-Item -Path $regPath -Recurse -Force -ErrorAction SilentlyContinue if (Test-Path $regPath) { Note '[注册表] 卸载登记项删除失败,待手动处理' } else { Note '[注册表] 已清理卸载登记项' } } Write-Host '' Write-Host '===== 清理报告 =====' $report | ForEach-Object { Write-Host $_ } if (-not $isAdmin) { Write-Host '' Write-Host '以下命令需要用管理员 PowerShell 补做:' Write-Host ' Stop-Service CodeSandboxPrivHelper -Force; sc.exe delete CodeSandboxPrivHelper' Write-Host ' Get-NetFirewallRule | ? {$_.Name -like "CodeSandbox-*" -or $_.DisplayGroup -like "CodeSandbox*"} | Remove-NetFirewallRule' Write-Host ' Remove-Item "$env:ProgramData\CodeSandbox" -Recurse -Force' } Write-Host '备份(如有)在桌面的 CodeSandbox-backup-* 目录,确认重装无问题后可手动删除。'