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

77 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
}
2025-03-03 14:16:53 -06:00
Write-Host "Attempting to install/update Winget`r"
try {
$wingetCmd = Get-Command winget -ErrorAction Stop
Write-Information "Attempting to update WinGet using WinGet..."
$result = Start-Process -FilePath "`"$($wingetCmd.Source)`"" -ArgumentList "install -e --accept-source-agreements --accept-package-agreements Microsoft.AppInstaller" -Wait -NoNewWindow -PassThru
if ($result.ExitCode -ne 0) {
throw "WinGet update failed with exit code: $($result.ExitCode)"
}
Write-Output "Refreshing Environment Variables...`n"
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
return
} catch {
Write-Information "WinGet not found or update failed. Attempting to install from Microsoft Store..."
}
try {
Write-Host "Attempting to repair WinGet using Repair-WinGetPackageManager..." -ForegroundColor Yellow
# Check if Windows version supports Repair-WinGetPackageManager (24H2 and above)
if ([System.Environment]::OSVersion.Version.Build -ge 26100) {
Repair-WinGetPackageManager -Force -Latest -Verbose
# Verify if repair was successful
$wingetCmd = Get-Command winget -ErrorAction Stop
Write-Host "WinGet repair successful!" -ForegroundColor Green
2025-03-03 14:16:53 -06:00
} else {
Write-Host "Repair-WinGetPackageManager is only available on Windows 24H2 and above. Your version doesn't support this method." -ForegroundColor Yellow
throw "Windows version not supported for repair method"
2025-03-03 14:16:53 -06:00
}
Write-Output "Refreshing Environment Variables...`n"
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
return
2025-03-03 14:16:53 -06:00
} catch {
Write-Error "All installation methods failed. Unable to install WinGet."
throw
2025-03-03 14:16:53 -06:00
}
} catch {
Write-Error "An error occurred during WinGet installation: $_"
2025-03-03 14:16:53 -06:00
throw
+3
2023-03-07 12:28:00 -08:00
}
+8
2024-03-28 12:39:23 -07:00
}