check-and-install-vc-runtime.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#----------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
#----------------------------------------------------------------------------------------------
# PowerShell script to install the VC++ runtime distribables.
# Uses the VcRedist PowerShell module.
# https://www.powershellgallery.com/packages/VcRedist
#

# Check VC++ 2013 runtime installation status.
$vcRegKey= "HKLM:\SOFTWARE\Microsoft\DevDiv\vc\Servicing\12.0\RuntimeAdditional"
$installStatus = (Get-ItemProperty -Path $vcRegKey).Install

if (($installStatus.length -eq 0) -Or ($installStatus -eq 0))
{
    Write-Host "Required VC++ 2013 x64 Runtimes are not exist."  -ForegroundColor Yellow
    Write-Host "Installing the VC++ 2013 x64 Runtime now." -ForegroundColor Yellow
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12

    # install VC runtime modules
    Import-Module VcRedist -ErrorAction Stop

    # Create download folder
    $tmpPath = -join($env:TEMP, "\VcRedist")
    New-Item -Path $tmpPath -ItemType Directory -Force -ErrorAction Stop | Out-Null

    Write-Host "Downloading the VC++ 2013 x64 Runtime ..." -ForegroundColor Yellow
    $VcList = Get-VcList | Where-Object {$_.Architecture -eq 'x64'} | Where-Object {$_.Name -like '*2013*'} | Get-VcRedist -Path $tmpPath

    Write-Host "Installing the VC++ 2013 x64 Runtime ..." -ForegroundColor Yellow
    $VcList | Install-VcRedist -Path $tmpPath

    # remove temporary files
    Remove-Item -Path $tmpPath -Force -Recurse
}