Public/Install-VcRedist.ps1

function Install-VcRedist {
    <#
        .EXTERNALHELP VcRedist-help.xml
    #>

    [CmdletBinding(SupportsShouldProcess = $true, HelpURI = "https://vcredist.com/install-vcredist/")]
    [OutputType([System.Management.Automation.PSObject])]
    param (
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline = $true,
            HelpMessage = "Pass a VcList object from Save-VcRedist.")]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSObject] $VcList,

        [Parameter(Mandatory = $false)]
        [System.Management.Automation.SwitchParameter] $Silent,

        [Parameter(Mandatory = $false)]
        [System.Management.Automation.SwitchParameter] $Force
    )

    begin {
        # Get script elevation status
        [System.Boolean] $Elevated = Test-Elevated
        if ($Elevated -eq $false) {
            $Msg = "Installing the Visual C++ Redistributables requires elevation. The current PowerShell session is not running as Administrator. Start Windows PowerShell by using the Run as Administrator option, and then try running the script again"
            throw [System.Management.Automation.ScriptRequiresException]::New($Msg)
        }

        # Get currently installed VcRedist versions
        $currentInstalled = Get-InstalledVcRedist
        $OperatingSystemArchitecture = Get-VcOperatingSystemArchitecture
        $InstallOrder = (Get-VcManifest -Source "Bundled").InstallOrder
        if ($null -eq $InstallOrder -or @($InstallOrder).Count -eq 0) {
            throw "The bundled manifest is missing InstallOrder."
        }
        $Items = [System.Collections.Generic.List[System.Object]]::new()
    }

    process {

        # Make sure that $VcList has the required properties
        if ((Test-VcListObject -VcList $VcList) -ne $true) {
            $Msg = "Required properties not found. Please ensure the output from Save-VcRedist is sent to this function. "
            throw [System.Management.Automation.PropertyNotFoundException]::New($Msg)
        }

        foreach ($Item in $VcList) {
            if ($InstallOrder -notcontains $Item.Release) {
                throw "Release '$($Item.Release)' is missing from the bundled manifest InstallOrder."
            }
            $Items.Add($Item)
        }
    }

    end {
        # Install releases in manifest order, then versions from oldest to newest.
        foreach ($VcRedist in ($Items | Sort-Object -Property @{ Expression = { [System.Array]::IndexOf($InstallOrder, $_.Release) } }, @{ Expression = { [System.Version]$_.Version } })) {

            $SupportedArchitecture = @(Get-VcSupportedArchitecture -SupportedArchitecture $VcRedist.SupportedArchitecture)

            # If already installed or the -Force parameter is not specified, skip
            if (($currentInstalled | Where-Object { $VcRedist.ProductCode -contains $_.ProductCode }) -and !($PSBoundParameters.ContainsKey("Force"))) {
                Write-Information -MessageData "VcRedist already installed: '$($VcRedist.Name) $($VcRedist.Version) $($VcRedist.Architecture)'" -InformationAction "Continue"
            }
            else {

                $ForceArchitecture = $Force -and $OperatingSystemArchitecture -eq "ARM64" -and $VcRedist.Architecture -eq "x64"
                if ($SupportedArchitecture -notcontains $OperatingSystemArchitecture -and !$ForceArchitecture) {
                    Write-Warning -Message "Architecture: '$($VcRedist.Name) $($VcRedist.Version) $($VcRedist.Architecture)' is not recommended on '$OperatingSystemArchitecture'. Use -Force to install."
                }
                else {

                    if (Test-Path -LiteralPath $VcRedist.Path -PathType "Leaf") {
                        Write-Verbose -Message "Install VcRedist: '$($VcRedist.Name) $($VcRedist.Version) $($VcRedist.Architecture)'"
                        if ($PSCmdlet.ShouldProcess("$($VcRedist.Path) $($VcRedist.Install)", "Install")) {

                            # Only run installers with a valid Microsoft signature
                            Assert-VcSignature -Path $VcRedist.Path | Out-Null

                            # Create parameters with -ArgumentList set based on Install/SilentInstall properties in the manifest
                            $params = @{
                                FilePath     = $VcRedist.Path
                                ArgumentList = if ($Silent) { $VcRedist.SilentInstall } else { $VcRedist.Install }
                                PassThru     = $true
                                Wait         = $true
                                NoNewWindow  = $true
                                ErrorAction  = "Stop"
                            }
                            $Result = Start-Process @params

                            # 3010: reboot required; 1641: reboot initiated; 1638: another version already installed
                            if (@(0, 1638, 1641, 3010) -contains $Result.ExitCode) {
                                Write-Verbose -Message "Installed: '$($VcRedist.Name) $($VcRedist.Version) $($VcRedist.Architecture)'; ExitCode: $($Result.ExitCode)"
                                if (@(1641, 3010) -contains $Result.ExitCode) {
                                    Write-Warning -Message "A restart is required to complete installation of '$($VcRedist.Name) $($VcRedist.Architecture)'."
                                }
                            }
                            else {
                                Write-Error -Message "Installation of '$($VcRedist.Name) $($VcRedist.Version) $($VcRedist.Architecture)' failed with exit code $($Result.ExitCode)."
                            }
                        }
                    }
                    else {
                        Write-Warning -Message "Cannot find: '$($VcRedist.Path)'. Download with Save-VcRedist."
                    }
                }
            }
        }
        # Get the installed Visual C++ Redistributables applications to return on the pipeline
        Write-Output -InputObject (Get-InstalledVcRedist)
    }
}