-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-winget.ps1
72 lines (66 loc) · 2.54 KB
/
install-winget.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#-----------------------------------------------------------------------------
#
# Copyright (c) 2025, Thierry Lelegard
# BSD-2-Clause license, see LICENSE.txt file
#
# Install WinGet package manager, if not already installed.
# See parameters documentation in install-common.ps1.
#
#-----------------------------------------------------------------------------
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[string]$Destination = "",
[switch]$ForceDownload = $false,
[switch]$GitHubActions = $false,
[switch]$NoInstall = $false,
[switch]$NoPause = $false
)
Write-Output "==== WinGet download and installation procedure"
. "$PSScriptRoot\install-common.ps1"
# A function to locate WinGet.
function Find-WinGet()
{
foreach ($dir in $env:Path.Split(';')) {
if (Test-Path "$dir\WinGet.exe") {
return "$dir\WinGet.exe"
}
}
return $null
}
# Install WinGet only when not found.
if (-not (Find-WinGet)) {
if (-not $IsAdmin) {
# Execution for non-admin user, recurse for admin part.
Recurse-Admin
}
else {
# See https://learn.microsoft.com/en-us/windows/package-manager/winget/
# We noticed intermittent failures, retry up to 3 times.
$retries = 3
Write-Output "PowerShell version: $($PSVersionTable.PSVersion.ToString())"
while (-not (Find-WinGet) -and $retries -gt 0) {
$retries--
if ($PSVersionTable.PSVersion.Major -lt 7) {
Write-Output "Installing NuGet ..."
Install-PackageProvider -Name "NuGet" -Force -ForceBootstrap -ErrorAction Continue
}
if (-not (Get-Module -Name Microsoft.WinGet.Client -ListAvailable)) {
Write-Output "Installing Microsoft.WinGet.Client PowerShell module ..."
Install-Module -Name Microsoft.WinGet.Client -AcceptLicense -Force -AllowClobber -Repository PSGallery -ErrorAction Continue
}
Import-Module -Name Microsoft.WinGet.Client
Write-Output "Installing WinGet ..."
try { Repair-WinGetPackageManager -AllUsers } catch {}
# We noticed some spurious asynchronous activity after installation.
# Let this background activity complete.
$timeout = 5
while (-not (Find-WinGet) -and $timeout -gt 0) {
$timeout--
Write-Output "WinGet not found, retrying in two seconds..."
Start-Sleep -Seconds 2
}
}
}
}
Write-Output "WinGet: $(Find-WinGet)"
Exit-Script