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 installed.
$vcRegKey= "HKLM:\SOFTWARE\Microsoft\DevDiv\vc\Servicing\12.0\RuntimeAdditional"
$vcRegName = "Install"
$installStatus = Get-ItemProperty -Path $vcRegKey -Name $vcRegName -ErrorAction Ignore | Out-Null

if (($installStatus.length -eq 0) -Or ($installStatus -eq 0))
{
    [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 VC++ runtime redistributables ..."
    $VcList = Get-VcList | Get-VcRedist -Path $tmpPath

    Write-Host "Installing VC++ runtimes ..."
    $VcList | Install-VcRedist -Path $tmpPath

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