Posted in

Winget – install on Windows 2019

You can install winget on Windows 2019 using this script – just open PowerShell ISE as administrator and run this script:

<#
.SYNOPSIS
    Installs winget (App Installer) on Windows Server 2019.
.DESCRIPTION
    - Installs VC++ Redistributable (required on WS2019)
    - Downloads latest DesktopAppInstaller msixbundle, dependencies and license
    - Provisions winget for all users via Add-AppxProvisionedPackage
    - Adds winget to system PATH
    Run from an elevated PowerShell session.
#>

#Requires -RunAsAdministrator
[CmdletBinding()]
param(
    [string]$WorkDir = "$env:TEMP\winget-install"
)

$ErrorActionPreference = 'Stop'
$ProgressPreference    = 'SilentlyContinue'   # speeds up Invoke-WebRequest dramatically
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# 1. Workspace --------------------------------------------------------------
New-Item -ItemType Directory -Path $WorkDir -Force | Out-Null
Set-Location $WorkDir

# 2. VC++ 2015-2022 x64 Redistributable (required on Server 2019) -----------
$vcKey = 'HKLM:\SOFTWARE\Microsoft\VisualStudio\14.0\VC\Runtimes\x64'
$vcInstalled = (Get-ItemProperty -Path $vcKey -ErrorAction SilentlyContinue).Installed -eq 1
if (-not $vcInstalled) {
    Write-Host 'Installing VC++ x64 redistributable...' -ForegroundColor Cyan
    Invoke-WebRequest -Uri 'https://aka.ms/vs/17/release/vc_redist.x64.exe' -OutFile 'vc_redist.x64.exe'
    Start-Process -FilePath '.\vc_redist.x64.exe' -ArgumentList '/install','/quiet','/norestart' -Wait
}

# 3. Resolve latest winget release assets -----------------------------------
Write-Host 'Querying latest winget-cli release...' -ForegroundColor Cyan
$release = Invoke-RestMethod 'https://api.github.com/repos/microsoft/winget-cli/releases/latest' `
    -Headers @{ 'User-Agent' = 'PowerShell' }

$bundle  = $release.assets | Where-Object name -Match 'Microsoft\.DesktopAppInstaller.*\.msixbundle$' | Select-Object -First 1
$deps    = $release.assets | Where-Object name -Match 'DesktopAppInstaller_Dependencies.*\.zip$'      | Select-Object -First 1
$license = $release.assets | Where-Object name -Match 'License.*\.xml$'                                | Select-Object -First 1

if (-not ($bundle -and $deps -and $license)) {
    throw "Required assets not found in release $($release.tag_name)"
}
Write-Host "Release: $($release.tag_name)" -ForegroundColor Green

# 4. Download ---------------------------------------------------------------
Invoke-WebRequest $bundle.browser_download_url  -OutFile $bundle.name
Invoke-WebRequest $deps.browser_download_url    -OutFile $deps.name
Invoke-WebRequest $license.browser_download_url -OutFile $license.name

# 5. Unpack and install x64 dependency packages -----------------------------
Expand-Archive -Path $deps.name -DestinationPath '.\Dependencies' -Force
Get-ChildItem -Path '.\Dependencies\x64' -Recurse -File |
    Where-Object Extension -in '.appx','.msix' |
    ForEach-Object {
        Write-Host "Installing dependency: $($_.Name)" -ForegroundColor Cyan
        Add-AppxPackage -Path $_.FullName -ErrorAction SilentlyContinue
    }

# 6. Provision winget for all users ----------------------------------------
Write-Host 'Provisioning DesktopAppInstaller...' -ForegroundColor Cyan
Add-AppxProvisionedPackage -Online `
    -PackagePath $bundle.name `
    -DependencyPackagePath (Get-ChildItem '.\Dependencies\x64' -Recurse -File |
        Where-Object Extension -in '.appx','.msix' | Select-Object -ExpandProperty FullName) `
    -LicensePath $license.name | Out-Null

# 7. Add winget to machine PATH (WindowsApps reparse point on WS2019) ------
$wingetDir = (Resolve-Path "$env:ProgramFiles\WindowsApps\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe" -ErrorAction SilentlyContinue |
              Sort-Object Path -Descending | Select-Object -First 1).Path
if ($wingetDir -and (Test-Path (Join-Path $wingetDir 'winget.exe'))) {
    $machinePath = [Environment]::GetEnvironmentVariable('Path','Machine')
    if ($machinePath -notlike "*$wingetDir*") {
        [Environment]::SetEnvironmentVariable('Path', "$machinePath;$wingetDir", 'Machine')
        Write-Host "Added to PATH: $wingetDir" -ForegroundColor Green
    }
    $env:Path = "$env:Path;$wingetDir"
}

# 8. Verify ----------------------------------------------------------------
Write-Host "Winget installed - restart Powershell window" -ForegroundColor Green

Microsoft Certified Trainer, Office 365, AWS, Azure and Cloud Expert-Architect. In the IT world for over than 20 years.

Apart from the main area of Microsoft Azure expert in the field of infrastructure servers Windows Server 2003-2019, Microsoft Active Directory, Hyper-V Private Cloud, IIS, System Center, SQL.

Private Cloud, System Center, Hyper-V, Open Stack Expert and all Microsoft products Expert. Linux Server administrator.

My Azure community projects:

https://mazeball.azurewebsites.net/
https://github.com/MariuszFerdyn?tab=repositories

More