@@ -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 " `n Winget is already installed.`r " - ForegroundColor Green
15- return
16- } elseif ($isWingetInstalled -eq " outdated" ) {
17- Write-Host " `n Winget is Outdated. Continuing with install.`r " - ForegroundColor Yellow
18- } else {
19- Write-Host " `n Winget 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}
0 commit comments