Skip to content

Commit 1b78cd1

Browse files
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 <[email protected]> Co-authored-by: Chris Titus <[email protected]>
1 parent a1907f5 commit 1b78cd1

File tree

2 files changed

+15
-130
lines changed

2 files changed

+15
-130
lines changed

functions/private/Install-WinUtilWinget.ps1

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,79 +2,20 @@ function Install-WinUtilWinget {
22
<#
33
44
.SYNOPSIS
5-
Installs Winget if it is not already installed.
5+
Installs Winget if not already installed.
66
77
.DESCRIPTION
8-
This function will download the latest version of Winget and install it. If Winget is already installed, it will do nothing.
8+
installs winget if needed
99
#>
10-
$isWingetInstalled = Test-WinUtilPackageManager -winget
11-
12-
try {
13-
if ($isWingetInstalled -eq "installed") {
14-
Write-Host "`nWinget is already installed.`r" -ForegroundColor Green
15-
return
16-
} elseif ($isWingetInstalled -eq "outdated") {
17-
Write-Host "`nWinget is Outdated. Continuing with install.`r" -ForegroundColor Yellow
18-
} else {
19-
Write-Host "`nWinget is not Installed. Continuing with install.`r" -ForegroundColor Red
20-
}
21-
22-
23-
# Gets the computer's information
24-
if ($null -eq $sync.ComputerInfo) {
25-
$ComputerInfo = Get-ComputerInfo -ErrorAction Stop
26-
} else {
27-
$ComputerInfo = $sync.ComputerInfo
28-
}
29-
30-
if (($ComputerInfo.WindowsVersion) -lt "1809") {
31-
# Checks if Windows Version is too old for Winget
32-
Write-Host "Winget is not supported on this version of Windows (Pre-1809)" -ForegroundColor Red
33-
return
34-
}
35-
36-
Write-Host "Attempting to install/update Winget`r"
37-
try {
38-
$wingetCmd = Get-Command winget -ErrorAction Stop
39-
Write-Information "Attempting to update WinGet using WinGet..."
40-
$result = Start-Process -FilePath "`"$($wingetCmd.Source)`"" -ArgumentList "install -e --accept-source-agreements --accept-package-agreements Microsoft.AppInstaller" -Wait -NoNewWindow -PassThru
41-
if ($result.ExitCode -ne 0) {
42-
throw "WinGet update failed with exit code: $($result.ExitCode)"
43-
}
44-
Write-Output "Refreshing Environment Variables...`n"
45-
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
46-
return
47-
} catch {
48-
Write-Information "WinGet not found or update failed. Attempting to install from Microsoft Store..."
49-
}
50-
try {
51-
Write-Host "Attempting to repair WinGet using Repair-WinGetPackageManager..." -ForegroundColor Yellow
52-
53-
# Check if Windows version supports Repair-WinGetPackageManager (24H2 and above)
54-
if ([System.Environment]::OSVersion.Version.Build -ge 26100) {
55-
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
56-
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
57-
Install-Module "Microsoft.WinGet.Client" -Force
58-
Import-Module Microsoft.WinGet.Client
59-
Repair-WinGetPackageManager -Force -Latest -Verbose
60-
# Verify if repair was successful
61-
$wingetCmd = Get-Command winget -ErrorAction Stop
62-
Write-Host "WinGet repair successful!" -ForegroundColor Green
63-
} else {
64-
Write-Host "Repair-WinGetPackageManager is only available on Windows 24H2 and above. Your version doesn't support this method." -ForegroundColor Yellow
65-
throw "Windows version not supported for repair method"
66-
}
67-
68-
Write-Output "Refreshing Environment Variables...`n"
69-
$ENV:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
70-
return
71-
72-
} catch {
73-
Write-Error "All installation methods failed. Unable to install WinGet."
74-
throw
75-
}
76-
} catch {
77-
Write-Error "An error occurred during WinGet installation: $_"
78-
throw
10+
if ((Test-WinUtilPackageManager -winget) -eq "installed") {
11+
return
7912
}
13+
14+
Write-Host "Winget is not Installed. Installing." -ForegroundColor Red
15+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
16+
17+
Install-PackageProvider -Name NuGet -Force
18+
Install-Module Microsoft.WinGet.Client -Force
19+
Import-Module Microsoft.WinGet.Client
20+
Repair-WinGetPackageManager
8021
}

functions/private/Test-WinUtilPackageManager.ps1

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,12 @@ function Test-WinUtilPackageManager {
1717
[System.Management.Automation.SwitchParameter]$choco
1818
)
1919

20-
$status = "not-installed"
21-
2220
if ($winget) {
23-
# Check if Winget is available while getting it's Version if it's available
24-
$wingetExists = $true
25-
try {
26-
$wingetInfo = winget --info
27-
# Extract the package version from the output
28-
$wingetVersionFull = ($wingetInfo | Select-String -Pattern 'Microsoft\.DesktopAppInstaller v\d+\.\d+\.\d+\.\d+').Matches.Value
29-
if ($wingetVersionFull) {
30-
$wingetVersionFull = $wingetVersionFull.Split(' ')[-1].TrimStart('v')
31-
} else {
32-
# Fallback in case the pattern isn't found
33-
$wingetVersionFull = ($wingetInfo | Select-String -Pattern 'Package Manager v\d+\.\d+\.\d+').Matches.Value.Split(' ')[-1]
34-
}
35-
} catch [System.Management.Automation.CommandNotFoundException], [System.Management.Automation.ApplicationFailedException] {
36-
Write-Warning "Winget was not found due to un-availability reasons"
37-
$wingetExists = $false
38-
} catch {
39-
Write-Warning "Winget was not found due to un-known reasons, The Stack Trace is:`n$($psitem.Exception.StackTrace)"
40-
$wingetExists = $false
41-
}
42-
43-
# If Winget is available, Parse it's Version and give proper information to Terminal Output.
44-
# If it isn't available, the return of this function will be "not-installed", indicating that
45-
# Winget isn't installed/available on The System.
46-
if ($wingetExists) {
47-
# Check if Preview Version
48-
if ($wingetVersionFull.Contains("-preview")) {
49-
$wingetVersion = $wingetVersionFull.Trim("-preview")
50-
$wingetPreview = $true
51-
} else {
52-
$wingetVersion = $wingetVersionFull
53-
$wingetPreview = $false
54-
}
55-
56-
# Check if Winget's Version is too old.
57-
$wingetCurrentVersion = [System.Version]::Parse($wingetVersion.Trim('v'))
58-
# Grabs the latest release of Winget from the GitHub API for version check process.
59-
$response = winget search -e Microsoft.AppInstaller --accept-source-agreements
60-
$wingetLatestVersion = ($response | Select-String -Pattern '\d+\.\d+\.\d+\.\d+').Matches.Value
61-
Write-Host "Latest Search Version: $wingetLatestVersion" -ForegroundColor White
62-
Write-Host "Current Installed Version: $wingetCurrentVersion" -ForegroundColor White
63-
$wingetOutdated = $wingetCurrentVersion -lt [System.Version]::Parse($wingetLatestVersion)
21+
if (Get-Command winget -ErrorAction SilentlyContinue) {
6422
Write-Host "===========================================" -ForegroundColor Green
6523
Write-Host "--- Winget is installed ---" -ForegroundColor Green
6624
Write-Host "===========================================" -ForegroundColor Green
67-
68-
if (!$wingetPreview) {
69-
Write-Host " - Winget is a release version." -ForegroundColor Green
70-
} else {
71-
Write-Host " - Winget is a preview version. Unexpected problems may occur." -ForegroundColor Yellow
72-
}
73-
74-
if (!$wingetOutdated) {
75-
Write-Host " - Winget is Up to Date" -ForegroundColor Green
76-
$status = "installed"
77-
} else {
78-
Write-Host " - Winget is Out of Date" -ForegroundColor Red
79-
$status = "outdated"
80-
}
25+
$status = "installed"
8126
} else {
8227
Write-Host "===========================================" -ForegroundColor Red
8328
Write-Host "--- Winget is not installed ---" -ForegroundColor Red
@@ -87,11 +32,10 @@ function Test-WinUtilPackageManager {
8732
}
8833

8934
if ($choco) {
90-
if ((Get-Command -Name choco -ErrorAction Ignore) -and ($chocoVersion = (Get-Item "$env:ChocolateyInstall\choco.exe" -ErrorAction Ignore).VersionInfo.ProductVersion)) {
35+
if (Get-Command choco -ErrorAction SilentlyContinue) {
9136
Write-Host "===========================================" -ForegroundColor Green
9237
Write-Host "--- Chocolatey is installed ---" -ForegroundColor Green
9338
Write-Host "===========================================" -ForegroundColor Green
94-
Write-Host "Version: v$chocoVersion" -ForegroundColor White
9539
$status = "installed"
9640
} else {
9741
Write-Host "===========================================" -ForegroundColor Red

0 commit comments

Comments
 (0)