From 5594752906867c757987a859ce826db85073e186 Mon Sep 17 00:00:00 2001 From: Olivier Date: Sun, 24 May 2026 11:14:12 +0200 Subject: [PATCH] Inline Initialize-Winget function into each script Each script is now self-contained and no longer dot-sources Initialize-Winget.ps1. The helper file is kept as reference. Co-Authored-By: Claude Sonnet 4.6 --- powershell/Install-Firefox.ps1 | 66 ++++++++++++++++++++++- powershell/Winget-Remove-BuiltInApps.ps1 | 67 +++++++++++++++++++++++- powershell/libres-softwares-install.ps1 | 67 +++++++++++++++++++++++- 3 files changed, 197 insertions(+), 3 deletions(-) diff --git a/powershell/Install-Firefox.ps1 b/powershell/Install-Firefox.ps1 index 7b2601a..50659d8 100644 --- a/powershell/Install-Firefox.ps1 +++ b/powershell/Install-Firefox.ps1 @@ -9,7 +9,71 @@ param( $ErrorActionPreference = "Stop" -. (Join-Path $PSScriptRoot "Initialize-Winget.ps1") +function Initialize-Winget { + if (Get-Command winget -ErrorAction SilentlyContinue) { + return + } + + Write-Host "winget not found in PATH. Attempting repair..." -ForegroundColor Yellow + + $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 + } + + $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: $_" + } + } + + $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 +} $IsAdministrator = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator diff --git a/powershell/Winget-Remove-BuiltInApps.ps1 b/powershell/Winget-Remove-BuiltInApps.ps1 index 42393a8..4dd6c45 100644 --- a/powershell/Winget-Remove-BuiltInApps.ps1 +++ b/powershell/Winget-Remove-BuiltInApps.ps1 @@ -1,7 +1,72 @@ # Requires running as administrator # Uninstalling Windows applications via winget -. (Join-Path $PSScriptRoot "Initialize-Winget.ps1") +function Initialize-Winget { + if (Get-Command winget -ErrorAction SilentlyContinue) { + return + } + + Write-Host "winget not found in PATH. Attempting repair..." -ForegroundColor Yellow + + $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 + } + + $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: $_" + } + } + + $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 +} + Initialize-Winget $Packages = @( diff --git a/powershell/libres-softwares-install.ps1 b/powershell/libres-softwares-install.ps1 index d81ce56..8013da7 100644 --- a/powershell/libres-softwares-install.ps1 +++ b/powershell/libres-softwares-install.ps1 @@ -6,7 +6,72 @@ param() $ErrorActionPreference = "Continue" -. (Join-Path $PSScriptRoot "Initialize-Winget.ps1") +function Initialize-Winget { + if (Get-Command winget -ErrorAction SilentlyContinue) { + return + } + + Write-Host "winget not found in PATH. Attempting repair..." -ForegroundColor Yellow + + $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 + } + + $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: $_" + } + } + + $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 +} + Initialize-Winget $IsAdministrator = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(