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 <noreply@anthropic.com>
This commit is contained in:
2026-05-24 11:14:12 +02:00
parent 17b3df2628
commit 5594752906
3 changed files with 197 additions and 3 deletions
+65 -1
View File
@@ -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