Last active
June 4, 2024 15:30
-
-
Save SandiyosDev/16ce2a887ac2935dc006f5979d98d206 to your computer and use it in GitHub Desktop.
This PowerShell script lets you install Winget on Windows editions (Mainly LTSC) without Microsoft Store prepackaged. To run this script, execute the following one-liner in PowerShell: "irm https://gist.githubusercontent.com/SandiyosDev/16ce2a887ac2935dc006f5979d98d206/raw/winget-installation-script.ps1/ | iex"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script automates the installation of Winget on Windows (Tested on Windows 10 IoT Enterprise LTSC 2021) and was last updated on 8/24/2023. | |
# It was designed according to the article: https://learn.microsoft.com/zh-tw/windows/iot/iot-enterprise/deployment/install-winget-windows-iot | |
# To run this script, execute the following one-liner in PowerShell: | |
# irm https://gist.githubusercontent.com/SandiyosDev/16ce2a887ac2935dc006f5979d98d206/raw/winget-installation-script.ps1/ | iex | |
# Greeting Message | |
Write-Host "Running script from https://gist.githubusercontent.com/SandiyosDev/16ce2a887ac2935dc006f5979d98d206/raw/winget-installation-script.ps1/ by SandiyosDev" | |
# Check if running as Administrator | |
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
Write-Host "Please run this script as an Administrator." | |
pause | |
exit | |
} | |
# Function to Download File | |
function DownloadFile { | |
param ( | |
[string]$url, | |
[string]$dest | |
) | |
$client = New-Object System.Net.WebClient | |
try { | |
$client.DownloadFile($url, $dest) | |
if (Test-Path -Path $dest) { | |
Write-Host "$dest downloaded successfully." | |
} | |
else { | |
Write-Host "Failed to download $dest." | |
pause | |
exit | |
} | |
} | |
catch { | |
Write-Host ("An error occurred downloading " + $url + ": " + $_.Exception.Message) | |
pause | |
exit | |
} | |
} | |
# Function to Detect Architecture | |
function DetectArchitecture { | |
$architecture = (Get-CimInstance -ClassName Win32_Processor).AddressWidth | |
$caption = (Get-CimInstance -ClassName Win32_Processor).Caption | |
if ($architecture -eq 64) { | |
if ($caption -like "*ARM64*") { | |
return "ARM64" | |
} | |
else { | |
return "x64" | |
} | |
} | |
else { | |
Write-Host "Unsupported architecture detected. The script supports only x64 and ARM64." | |
pause | |
exit | |
} | |
} | |
# Main Code | |
$architecture = DetectArchitecture | |
Write-Host "Detected architecture: $architecture" | |
$tempDir = "$env:TEMP\WingetInstall" | |
New-Item -Path $tempDir -ItemType Directory -Force | |
# Define URLs based on the detected architecture | |
$VCLibsUrl = if ($architecture -eq "x64") { "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx" } else { "https://aka.ms/Microsoft.VCLibs.arm64.14.00.Desktop.appx" } | |
$wingetBundleUrl = "https://github.com/microsoft/winget-cli/releases/download/v1.7.11261/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle" | |
$licenseUrl = "https://github.com/microsoft/winget-cli/releases/download/v1.7.11261/64b03c63cd5d4be8a28e8a271ff853cc_License1.xml" | |
$xamlUrl = "https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.0" | |
# Download the files | |
DownloadFile $VCLibsUrl "$tempDir\VCLibs.appx" | |
DownloadFile $wingetBundleUrl "$tempDir\Winget.msixbundle" | |
DownloadFile $licenseUrl "$tempDir\License1.xml" | |
DownloadFile $xamlUrl "$tempDir\Microsoft.UI.Xaml.2.7.0.zip" | |
# Expand the XAML zip | |
[System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null | |
[System.IO.Compression.ZipFile]::ExtractToDirectory("$tempDir\Microsoft.UI.Xaml.2.7.0.zip", "$tempDir\Xaml") | |
# Installing components | |
Add-AppxPackage -Path "$tempDir\VCLibs.appx" | |
Add-AppxPackage -Path "$tempDir\Xaml\tools\AppX\$architecture\release\Microsoft.UI.XAML.2.7.appx" | |
Add-AppxPackage -Path "$tempDir\Winget.msixbundle" | |
Add-AppxProvisionedPackage -Online -PackagePath "$tempDir\Winget.msixbundle" -LicensePath "$tempDir\License1.xml" | |
# Cleaning up temporary files | |
Remove-Item -Path $tempDir -Recurse -Force | |
Write-Host "Installation complete. Testing 'winget' command..." | |
if (winget --version) { | |
winget | |
} else { | |
Write-Host "Winget installation failed. Please review the installation steps and try again." | |
pause | |
exit | |
} | |
# DISCLAIMER: This script is provided "as is" without warranty of any kind. Use it at your own risk. | |
# WARNING: Always review scripts from the internet before running them. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment