Skip to content

Instantly share code, notes, and snippets.

@Mkeefeus
Last active January 25, 2025 21:29
Show Gist options
  • Save Mkeefeus/5fb1e946a5e732e7d6889ffa43515333 to your computer and use it in GitHub Desktop.
Save Mkeefeus/5fb1e946a5e732e7d6889ffa43515333 to your computer and use it in GitHub Desktop.
Installs latest github release of WinGet on Windows
Function Install-WinGet {
##########################################################################################################
<#
.SYNOPSIS
Installs the latest github release of WinGet to the local machine.
.DESCRIPTION
This function downloads the latest release of WinGet from the github repository and installs it to the local machine.
The function will download the msixbundle and dependencies zip file to a temporary directory, extract the dependencies
and install the msixbundle. The temporary files will be removed after the installation is complete.
.EXAMPLE
Install-WinGet
#>
##########################################################################################################
[CmdletBinding()]
$TempDir = [System.IO.Path]::GetTempPath() + "Install-WinGet\"
if (-Not (Test-Path -Path "$TempDir")) {
New-Item -ItemType Directory -Path $TempDir | Out-Null
}
try {
Write-Host "Downloading WinGet. This may take a few minutes..."
Invoke-WebRequest -Uri "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -OutFile "$($TempDir)Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
Invoke-WebRequest -Uri "https://github.com/microsoft/winget-cli/releases/latest/download/DesktopAppInstaller_Dependencies.zip" -OutFile "$($TempDir)DesktopAppInstaller_Dependencies.zip"
}
catch {
Write-Host "Failed to download WinGet. Please check your internet connection and try again. Error: $_"
return
}
try {
Write-Host "Extracting WinGet dependencies..."
Expand-Archive -Path "$($TempDir)DesktopAppInstaller_Dependencies.zip" -DestinationPath "$($TempDir)Dependencies"
}
catch {
Write-Host "Failed to extract WinGet dependencies. Error: $_"
return
}
try {
Write-Host "Installing WinGet..."
$DependencyFiles = Get-ChildItem -Path "$TempDir\Dependencies\x64" -File | Select-Object -ExpandProperty FullName
Add-AppxPackage -Path "$($TempDir)Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" -DependencyPath $DependencyFiles
}
catch {
Write-Host "Failed to install WinGet msixbundle. Error: $_"
return
}
Write-Host "WinGet installed successfully."
try {
Remove-Item -Path $TempDir -Recurse -Force
}
catch {
Write-Host "Failed to remove temporary files. Please delete the folder $TempDir manually."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment