Files
winutil/functions/private/Install-WinUtilWinget.ps1
T

69 lines
3.4 KiB
PowerShell
Raw Normal View History

+3
2023-03-07 12:28:00 -08:00
function Install-WinUtilWinget {
<#
2023-10-19 17:12:55 -05:00
.SYNOPSIS
+8
2024-03-28 12:39:23 -07:00
Installs Winget if it is not already installed.
2023-10-19 17:12:55 -05:00
+12
2023-11-28 16:11:11 -06:00
.DESCRIPTION
+8
2024-03-28 12:39:23 -07:00
This function will download the latest version of Winget and install it. If Winget is already installed, it will do nothing.
+3
2023-03-07 12:28:00 -08:00
#>
$isWingetInstalled = Test-WinUtilPackageManager -winget
try {
if ($isWingetInstalled -eq "installed") {
Write-Host "`nWinget is already installed.`r" -ForegroundColor Green
+3
2023-03-07 12:28:00 -08:00
return
} elseif ($isWingetInstalled -eq "outdated") {
Write-Host "`nWinget is Outdated. Continuing with install.`r" -ForegroundColor Yellow
} else {
Write-Host "`nWinget is not Installed. Continuing with install.`r" -ForegroundColor Red
+3
2023-03-07 12:28:00 -08:00
}
2023-10-19 17:12:55 -05:00
# Gets the computer's information
if ($null -eq $sync.ComputerInfo) {
+3
2023-03-07 12:28:00 -08:00
$ComputerInfo = Get-ComputerInfo -ErrorAction Stop
} else {
+3
2023-03-07 12:28:00 -08:00
$ComputerInfo = $sync.ComputerInfo
}
if (($ComputerInfo.WindowsVersion) -lt "1809") {
+8
2024-03-28 12:39:23 -07:00
# Checks if Windows Version is too old for Winget
Write-Host "Winget is not supported on this version of Windows (Pre-1809)" -ForegroundColor Red
+3
2023-03-07 12:28:00 -08:00
return
}
# Install Winget via GitHub method.
# Used part of my own script with some modification: ruxunderscore/windows-initialization
Write-Host "Downloading Winget Prerequsites`n"
Get-WinUtilWingetPrerequisites
Write-Host "Downloading Winget and License File`r"
Get-WinUtilWingetLatest
Write-Host "Installing Winget w/ Prerequsites`r"
Add-AppxProvisionedPackage -Online -PackagePath $ENV:TEMP\Microsoft.DesktopAppInstaller.msixbundle -DependencyPackagePath $ENV:TEMP\Microsoft.VCLibs.x64.Desktop.appx, $ENV:TEMP\Microsoft.UI.Xaml.x64.appx -LicensePath $ENV:TEMP\License1.xml
Write-Host "Manually adding Winget Sources, from Winget CDN."
Add-AppxPackage -Path https://cdn.winget.microsoft.com/cache/source.msix #Seems some installs of Winget don't add the repo source, this should makes sure that it's installed every time.
Write-Host "Winget Installed" -ForegroundColor Green
+1
2024-04-20 16:38:43 -05:00
Write-Host "Enabling NuGet and Module..."
Install-PackageProvider -Name NuGet -Force
Install-Module -Name Microsoft.WinGet.Client -Force
# Winget only needs a refresh of the environment variables to be used.
Write-Output "Refreshing Environment Variables...`n"
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
} catch {
Write-Host "Failure detected while installing via GitHub method. Continuing with Chocolatey method as fallback." -ForegroundColor Red
# In case install fails via GitHub method.
try {
+26
2024-06-04 20:27:27 -07:00
# Install Choco if not already present
Install-WinUtilChoco
Start-Process -Verb runas -FilePath powershell.exe -ArgumentList "choco install winget-cli"
Write-Host "Winget Installed" -ForegroundColor Green
Write-Output "Refreshing Environment Variables...`n"
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
} catch {
throw [WingetFailedInstall]::new('Failed to install!')
+8
2024-03-28 12:39:23 -07:00
}
+3
2023-03-07 12:28:00 -08:00
}
+8
2024-03-28 12:39:23 -07:00
}