2023-03-07 12:28:00 -08:00
function Install-WinUtilWinget {
<#
2023-10-19 17:12:55 -05:00
. SYNOPSIS
2024-03-28 12:39:23 -07:00
Installs Winget if it is not already installed.
2023-10-19 17:12:55 -05:00
2023-11-28 16:11:11 -06:00
. DESCRIPTION
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.
2023-03-07 12:28:00 -08:00
#>
2024-03-30 11:46:29 -05:00
$isWingetInstalled = Test-WinUtilPackageManager -winget
2024-08-06 23:35:17 +03:00
try {
2024-03-30 11:46:29 -05:00
if ( $isWingetInstalled -eq "installed" ) {
Write-Host " `n Winget is already installed. `r " -ForegroundColor Green
2023-03-07 12:28:00 -08:00
return
2024-03-30 11:46:29 -05:00
} elseif ( $isWingetInstalled -eq "outdated" ) {
Write-Host " `n Winget is Outdated. Continuing with install. `r " -ForegroundColor Yellow
} else {
Write-Host " `n Winget is not Installed. Continuing with install. `r " -ForegroundColor Red
2023-03-07 12:28:00 -08:00
}
2024-07-25 23:19:45 +02:00
2023-10-19 17:12:55 -05:00
# Gets the computer's information
2024-08-06 23:35:17 +03:00
if ( $null -eq $sync . ComputerInfo ) {
2023-03-07 12:28:00 -08:00
$ComputerInfo = Get-ComputerInfo -ErrorAction Stop
2024-03-30 11:46:29 -05:00
} else {
2023-03-07 12:28:00 -08:00
$ComputerInfo = $sync . ComputerInfo
}
if (( $ComputerInfo . WindowsVersion ) -lt "1809" ) {
2024-03-28 12:39:23 -07:00
# Checks if Windows Version is too old for Winget
2024-03-30 11:46:29 -05:00
Write-Host "Winget is not supported on this version of Windows (Pre-1809)" -ForegroundColor Red
2023-03-07 12:28:00 -08:00
return
}
2024-03-30 11:46:29 -05:00
# 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
2024-08-06 23:35:17 +03:00
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.
2024-03-30 11:46:29 -05:00
Write-Host "Winget Installed" -ForegroundColor Green
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
2024-03-30 11:46:29 -05:00
# 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" )
2024-08-06 23:35:17 +03:00
} catch {
2024-03-30 11:46:29 -05:00
Write-Host "Failure detected while installing via GitHub method. Continuing with Chocolatey method as fallback." -ForegroundColor Red
# In case install fails via GitHub method.
2024-08-06 23:35:17 +03:00
try {
2024-06-04 20:27:27 -07:00
# Install Choco if not already present
Install-WinUtilChoco
2024-03-30 11:46:29 -05:00
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" )
2024-08-06 23:35:17 +03:00
} catch {
2024-03-30 11:46:29 -05:00
throw [ WingetFailedInstall ]:: new ( 'Failed to install!' )
2024-03-28 12:39:23 -07:00
}
2023-03-07 12:28:00 -08:00
}
2024-07-25 23:19:45 +02:00
2024-03-28 12:39:23 -07:00
}