重装 Visual C++ 的完整流程-PowerShell 版

bash 复制代码
#Requires -RunAsAdministrator
<#
.SYNOPSIS
    重装 Visual C++ 运行库(2005 ~ 2026)

.DESCRIPTION
    1. 使用 VisualCppRedist AIO 卸载全部已检测到的运行库
    2. 使用 VisualCppRedist AIO 静默安装全部运行库
    3. 使用微软官方 VC_redist 安装包补充安装 2015-2022 (x64/x86)

.NOTES
    请将本脚本与以下安装包放在同一目录后运行:
      - VisualCppRedist_AIO_x86_x64.exe
      - VC_redist.x64.exe
      - VC_redist.x86.exe

    右键「以管理员身份运行 PowerShell」,然后执行:
      Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
      .\Reinstall-VisualCpp.ps1
#>

$ErrorActionPreference = 'Stop'

$ScriptDir = $PSScriptRoot
$AioInstaller = Join-Path $ScriptDir 'VisualCppRedist_AIO_x86_x64.exe'
$VcRedistX64 = Join-Path $ScriptDir 'VC_redist.x64.exe'
$VcRedistX86 = Join-Path $ScriptDir 'VC_redist.x86.exe'

function Write-Step {
    param([string]$Message)
    Write-Host ""
    Write-Host "==> $Message" -ForegroundColor Cyan
}

function Invoke-Installer {
    param(
        [string]$Path,
        [string[]]$Arguments,
        [string]$Label
    )

    if (-not (Test-Path -LiteralPath $Path)) {
        throw "找不到安装包:$Path"
    }

    Write-Host "    运行: $(Split-Path -Leaf $Path) $($Arguments -join ' ')"
    $process = Start-Process -FilePath $Path -ArgumentList $Arguments -Wait -PassThru

    if ($process.ExitCode -ne 0) {
        throw "$Label 失败,退出码: $($process.ExitCode)"
    }

    Write-Host "    $Label 完成 (退出码 0)" -ForegroundColor Green
}

Write-Host "========================================" -ForegroundColor Yellow
Write-Host "  Visual C++ 运行库 一键重装脚本" -ForegroundColor Yellow
Write-Host "  安装包目录: $ScriptDir" -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow

Write-Step "第 1/3 步:卸载全部 Visual C++ 运行库"
Invoke-Installer -Path $AioInstaller -Arguments @('/aiR', '/gm2') -Label "AIO 卸载"

Write-Step "第 2/3 步:安装全部 Visual C++ 运行库 (AIO)"
Invoke-Installer -Path $AioInstaller -Arguments @('/ai', '/gm2') -Label "AIO 安装"

Write-Step "第 3/3 步:安装微软官方 2015-2022 运行库"
Invoke-Installer -Path $VcRedistX64 -Arguments @('/install', '/quiet', '/norestart') -Label "VC_redist x64"
Invoke-Installer -Path $VcRedistX86 -Arguments @('/install', '/quiet', '/norestart') -Label "VC_redist x86"

Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "  全部完成!建议重启电脑使变更生效。" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green

保存成.Reinstall-VisualCpp.ps1文件,以管理员身份打开 PowerShell,执行:

bash 复制代码
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
C:\Users\admin\Downloads\Reinstall-VisualCpp.ps1

注意,三个安装包要放在同一文件夹下:

运行完成后建议重启电脑。