check-and-install-vc-runtime.ps1

#----------------------------------------------------------------------------------------------
# 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 do 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
}