mirror of
https://github.com/ChrisTitusTech/winutil
synced 2026-02-04 06:50:09 +00:00
Update-Install-WinUtilWinget.ps1 and remove upgrading winget (#3839)
* Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Test-WinUtilPackageManager.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Update Install-WinUtilWinget.ps1 * Change error action to SilentlyContinue for commands * Fix indentation in Test-WinUtilPackageManager.ps1 --------- Co-authored-by: Gabi <218829269+Gabynun@users.noreply.github.com> Co-authored-by: Chris Titus <contact@christitus.com>
This commit is contained in:
@@ -2,79 +2,20 @@ function Install-WinUtilWinget {
|
||||
<#
|
||||
|
||||
.SYNOPSIS
|
||||
Installs Winget if it is not already installed.
|
||||
Installs Winget if not already installed.
|
||||
|
||||
.DESCRIPTION
|
||||
This function will download the latest version of Winget and install it. If Winget is already installed, it will do nothing.
|
||||
installs winget if needed
|
||||
#>
|
||||
$isWingetInstalled = Test-WinUtilPackageManager -winget
|
||||
|
||||
try {
|
||||
if ($isWingetInstalled -eq "installed") {
|
||||
Write-Host "`nWinget is already installed.`r" -ForegroundColor Green
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
# Gets the computer's information
|
||||
if ($null -eq $sync.ComputerInfo) {
|
||||
$ComputerInfo = Get-ComputerInfo -ErrorAction Stop
|
||||
} else {
|
||||
$ComputerInfo = $sync.ComputerInfo
|
||||
}
|
||||
|
||||
if (($ComputerInfo.WindowsVersion) -lt "1809") {
|
||||
# Checks if Windows Version is too old for Winget
|
||||
Write-Host "Winget is not supported on this version of Windows (Pre-1809)" -ForegroundColor Red
|
||||
return
|
||||
}
|
||||
|
||||
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) {
|
||||
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
|
||||
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
|
||||
Install-Module "Microsoft.WinGet.Client" -Force
|
||||
Import-Module Microsoft.WinGet.Client
|
||||
Repair-WinGetPackageManager -Force -Latest -Verbose
|
||||
# Verify if repair was successful
|
||||
$wingetCmd = Get-Command winget -ErrorAction Stop
|
||||
Write-Host "WinGet repair successful!" -ForegroundColor Green
|
||||
} 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"
|
||||
}
|
||||
|
||||
Write-Output "Refreshing Environment Variables...`n"
|
||||
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
|
||||
return
|
||||
|
||||
} catch {
|
||||
Write-Error "All installation methods failed. Unable to install WinGet."
|
||||
throw
|
||||
}
|
||||
} catch {
|
||||
Write-Error "An error occurred during WinGet installation: $_"
|
||||
throw
|
||||
if ((Test-WinUtilPackageManager -winget) -eq "installed") {
|
||||
return
|
||||
}
|
||||
|
||||
Write-Host "Winget is not Installed. Installing." -ForegroundColor Red
|
||||
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
|
||||
|
||||
Install-PackageProvider -Name NuGet -Force
|
||||
Install-Module Microsoft.WinGet.Client -Force
|
||||
Import-Module Microsoft.WinGet.Client
|
||||
Repair-WinGetPackageManager
|
||||
}
|
||||
|
||||
@@ -17,67 +17,12 @@ function Test-WinUtilPackageManager {
|
||||
[System.Management.Automation.SwitchParameter]$choco
|
||||
)
|
||||
|
||||
$status = "not-installed"
|
||||
|
||||
if ($winget) {
|
||||
# Check if Winget is available while getting it's Version if it's available
|
||||
$wingetExists = $true
|
||||
try {
|
||||
$wingetInfo = winget --info
|
||||
# Extract the package version from the output
|
||||
$wingetVersionFull = ($wingetInfo | Select-String -Pattern 'Microsoft\.DesktopAppInstaller v\d+\.\d+\.\d+\.\d+').Matches.Value
|
||||
if ($wingetVersionFull) {
|
||||
$wingetVersionFull = $wingetVersionFull.Split(' ')[-1].TrimStart('v')
|
||||
} else {
|
||||
# Fallback in case the pattern isn't found
|
||||
$wingetVersionFull = ($wingetInfo | Select-String -Pattern 'Package Manager v\d+\.\d+\.\d+').Matches.Value.Split(' ')[-1]
|
||||
}
|
||||
} catch [System.Management.Automation.CommandNotFoundException], [System.Management.Automation.ApplicationFailedException] {
|
||||
Write-Warning "Winget was not found due to un-availability reasons"
|
||||
$wingetExists = $false
|
||||
} catch {
|
||||
Write-Warning "Winget was not found due to un-known reasons, The Stack Trace is:`n$($psitem.Exception.StackTrace)"
|
||||
$wingetExists = $false
|
||||
}
|
||||
|
||||
# If Winget is available, Parse it's Version and give proper information to Terminal Output.
|
||||
# If it isn't available, the return of this function will be "not-installed", indicating that
|
||||
# Winget isn't installed/available on The System.
|
||||
if ($wingetExists) {
|
||||
# Check if Preview Version
|
||||
if ($wingetVersionFull.Contains("-preview")) {
|
||||
$wingetVersion = $wingetVersionFull.Trim("-preview")
|
||||
$wingetPreview = $true
|
||||
} else {
|
||||
$wingetVersion = $wingetVersionFull
|
||||
$wingetPreview = $false
|
||||
}
|
||||
|
||||
# Check if Winget's Version is too old.
|
||||
$wingetCurrentVersion = [System.Version]::Parse($wingetVersion.Trim('v'))
|
||||
# Grabs the latest release of Winget from the GitHub API for version check process.
|
||||
$response = winget search -e Microsoft.AppInstaller --accept-source-agreements
|
||||
$wingetLatestVersion = ($response | Select-String -Pattern '\d+\.\d+\.\d+\.\d+').Matches.Value
|
||||
Write-Host "Latest Search Version: $wingetLatestVersion" -ForegroundColor White
|
||||
Write-Host "Current Installed Version: $wingetCurrentVersion" -ForegroundColor White
|
||||
$wingetOutdated = $wingetCurrentVersion -lt [System.Version]::Parse($wingetLatestVersion)
|
||||
if (Get-Command winget -ErrorAction SilentlyContinue) {
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "--- Winget is installed ---" -ForegroundColor Green
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
|
||||
if (!$wingetPreview) {
|
||||
Write-Host " - Winget is a release version." -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host " - Winget is a preview version. Unexpected problems may occur." -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
if (!$wingetOutdated) {
|
||||
Write-Host " - Winget is Up to Date" -ForegroundColor Green
|
||||
$status = "installed"
|
||||
} else {
|
||||
Write-Host " - Winget is Out of Date" -ForegroundColor Red
|
||||
$status = "outdated"
|
||||
}
|
||||
$status = "installed"
|
||||
} else {
|
||||
Write-Host "===========================================" -ForegroundColor Red
|
||||
Write-Host "--- Winget is not installed ---" -ForegroundColor Red
|
||||
@@ -87,11 +32,10 @@ function Test-WinUtilPackageManager {
|
||||
}
|
||||
|
||||
if ($choco) {
|
||||
if ((Get-Command -Name choco -ErrorAction Ignore) -and ($chocoVersion = (Get-Item "$env:ChocolateyInstall\choco.exe" -ErrorAction Ignore).VersionInfo.ProductVersion)) {
|
||||
if (Get-Command choco -ErrorAction SilentlyContinue) {
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "--- Chocolatey is installed ---" -ForegroundColor Green
|
||||
Write-Host "===========================================" -ForegroundColor Green
|
||||
Write-Host "Version: v$chocoVersion" -ForegroundColor White
|
||||
$status = "installed"
|
||||
} else {
|
||||
Write-Host "===========================================" -ForegroundColor Red
|
||||
|
||||
Reference in New Issue
Block a user