Add winget repair helper and remove winget configure --enable prerequisite

Initialize-Winget.ps1 refreshes PATH, re-registers App Installer and
falls back to the provisioned package before exiting with a Store link.
All scripts that call winget now dot-source this helper instead of
failing on a missing winget.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-24 10:54:56 +02:00
parent 79ea61f445
commit 64352817f5
6 changed files with 84 additions and 26 deletions
+72
View File
@@ -0,0 +1,72 @@
# Shared helper: ensures winget is accessible before use.
# Dot-source this script and call Initialize-Winget at startup.
# Repair operations require administrator rights.
function Initialize-Winget {
if (Get-Command winget -ErrorAction SilentlyContinue) {
return
}
Write-Host "winget not found in PATH. Attempting repair..." -ForegroundColor Yellow
# Refresh PATH from registry in case a recent install is not yet visible
$MachinePath = [System.Environment]::GetEnvironmentVariable("PATH", "Machine")
$UserPath = [System.Environment]::GetEnvironmentVariable("PATH", "User")
$env:PATH = $MachinePath + ";" + $UserPath
if (Get-Command winget -ErrorAction SilentlyContinue) {
Write-Host "winget available after PATH refresh." -ForegroundColor Green
return
}
# Try to re-register the installed App Installer package
$AppInstaller = Get-AppxPackage -Name "Microsoft.DesktopAppInstaller" -ErrorAction SilentlyContinue
if (-not $AppInstaller) {
$AppInstaller = Get-AppxPackage -AllUsers -Name "Microsoft.DesktopAppInstaller" -ErrorAction SilentlyContinue
}
if ($AppInstaller) {
Write-Host "Re-registering App Installer..." -ForegroundColor Yellow
try {
$Manifest = Join-Path $AppInstaller.InstallLocation "AppxManifest.xml"
Add-AppxPackage -DisableDevelopmentMode -Register $Manifest -ErrorAction Stop
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("PATH", "User")
if (Get-Command winget -ErrorAction SilentlyContinue) {
Write-Host "winget repaired." -ForegroundColor Green
return
}
}
catch {
Write-Warning "Re-registration failed: $_"
}
}
# Try to reinstall from the provisioned (staged) package on this machine
$StagedPackage = Get-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -eq "Microsoft.DesktopAppInstaller" } |
Select-Object -First 1
if ($StagedPackage) {
Write-Host "Installing App Installer from provisioned package..." -ForegroundColor Yellow
try {
Add-AppxPackage -Path $StagedPackage.PackagePath -ErrorAction Stop
$env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" +
[System.Environment]::GetEnvironmentVariable("PATH", "User")
if (Get-Command winget -ErrorAction SilentlyContinue) {
Write-Host "winget installed." -ForegroundColor Green
return
}
}
catch {
Write-Warning "Installation from provisioned package failed: $_"
}
}
Write-Error ("winget is not available and could not be repaired automatically.`n" +
"Install App Installer from the Microsoft Store: " +
"ms-windows-store://pdp/?productid=9nblggh4nns1")
exit 1
}
+3 -4
View File
@@ -9,6 +9,8 @@ param(
$ErrorActionPreference = "Stop"
. (Join-Path $PSScriptRoot "Initialize-Winget.ps1")
$IsAdministrator = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator
)
@@ -23,10 +25,7 @@ function Install-FirefoxEsr {
[string]$PackageId
)
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Error "winget was not found. Install App Installer or enable winget before running this script."
exit 1
}
Initialize-Winget
Write-Host "Installing Firefox ESR with winget: $PackageId" -ForegroundColor Cyan
+3
View File
@@ -1,6 +1,9 @@
# Requires running as administrator
# Uninstalling Windows applications via winget
. (Join-Path $PSScriptRoot "Initialize-Winget.ps1")
Initialize-Winget
$Packages = @(
"Windows Web Experience Pack",
"Xbox",
+2 -6
View File
@@ -1,12 +1,8 @@
# Script d'installation de logiciels avec winget
# Permet de choisir les logiciels a installer
# Verification que winget est installe
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Host "ERREUR: winget n'est pas installe sur ce systeme." -ForegroundColor Red
Write-Host "Veuillez installer le 'App Installer' depuis le Microsoft Store." -ForegroundColor Yellow
exit 1
}
. (Join-Path $PSScriptRoot "Initialize-Winget.ps1")
Initialize-Winget
Write-Host "===========================================================" -ForegroundColor Cyan
Write-Host " Script d'installation de logiciels avec winget" -ForegroundColor Cyan