From 19d6022c26b95fc64559cb638a7f6b3af4b521f5 Mon Sep 17 00:00:00 2001 From: dinger1986 Date: Sun, 24 May 2026 12:45:07 +0100 Subject: [PATCH 1/2] Refactor WinGet script to streamline modes and error handling Removed 'update' and 'show' modes from the parameter options and adjusted logic for default mode assignment. Updated error handling for missing package names and winget executable. --- scripts/Win_WinGet_Manage_Apps.ps1 | 85 +++++++++++++++++++----------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/scripts/Win_WinGet_Manage_Apps.ps1 b/scripts/Win_WinGet_Manage_Apps.ps1 index f92654a6..e2080aea 100644 --- a/scripts/Win_WinGet_Manage_Apps.ps1 +++ b/scripts/Win_WinGet_Manage_Apps.ps1 @@ -4,7 +4,7 @@ .DESCRIPTION For installing packages using winget. .PARAMETER Mode - 6 options: install, uninstall, search, update, show or upgrade. + 4 options: install, uninstall, search or upgrade. .PARAMETER PackageName Use this to specify which software to install eg: PackageName google.chrome .EXAMPLE @@ -13,10 +13,6 @@ -Mode upgrade .EXAMPLE -Mode uninstall -PackageName google.chrome - .EXAMPLE - -Mode update -PackageName google.chrome - .EXAMPLE (to show updates available) - -Mode show .NOTES 9/2021 v1 Initial release by @silversword411 and @bradhawkins 11/14/2021 v1.1 Fixing typos and logic flow @@ -24,46 +20,75 @@ #> param ( - [string] $PackageName, + [string] $PackageName = "", [string] $Mode = "install" ) -$wingetloc=(Get-Childitem -Path "C:\Program Files\WindowsApps" -Include winget.exe -Recurse -ErrorAction SilentlyContinue | Select-Object -Last 1 | %{$_.FullName} | Split-Path) -cd $wingetloc - $ErrorCount = 0 -if ($Mode -eq "show") { - .\winget.exe upgrade --accept-source-agreements - Exit 0 +if ([string]::IsNullOrWhiteSpace($Mode)) { + $Mode = "install" } -if ($Mode -ne "update" -and !$PackageName) { - write-output "No package name provided, please include Example: `"-PackageName google.chrome`" `n" +$Mode = $Mode.ToLower() + +$ValidModes = @("install", "uninstall", "search", "update", "show", "upgrade") + +if ($Mode -notin $ValidModes) { + Write-Output "Invalid mode: $Mode" Exit 1 } -if ($Mode -eq "upgrade") { - .\winget.exe upgrade --all --accept-source-agreements - Exit 0 -} +$wingetloc = Get-ChildItem -Path "C:\Program Files\WindowsApps" -Include winget.exe -Recurse -ErrorAction SilentlyContinue | + Select-Object -Last 1 | + ForEach-Object { $_.FullName } | + Split-Path -if ($Mode -eq "update") { - .\winget.exe upgrade $PackageName --accept-source-agreements - Exit 0 +if ([string]::IsNullOrWhiteSpace($wingetloc)) { + Write-Output "winget.exe not found." + Exit 1 } -if ($Mode -eq "search") { - .\winget.exe search $PackageName --accept-source-agreements - Exit 0 +$winget = Join-Path $wingetloc "winget.exe" + +if (!(Test-Path $winget)) { + Write-Output "winget.exe not found at $winget" + Exit 1 } -if ($Mode -eq "install") { - .\winget.exe install $PackageName --accept-source-agreements --accept-package-agreements - Exit 0 +if ($Mode -notin @("show", "upgrade") -and [string]::IsNullOrWhiteSpace($PackageName)) { + Write-Output "No package name provided. Example: -PackageName Google.Chrome" + Exit 1 } -if ($Mode -eq "uninstall") { - .\winget.exe uninstall $PackageName --accept-source-agreements - Exit 0 +switch ($Mode) { + "show" { + & $winget upgrade --accept-source-agreements + Exit $LASTEXITCODE + } + + "upgrade" { + & $winget upgrade --all --accept-source-agreements --accept-package-agreements + Exit $LASTEXITCODE + } + + "update" { + & $winget upgrade --id $PackageName --accept-source-agreements --accept-package-agreements + Exit $LASTEXITCODE + } + + "search" { + & $winget search $PackageName --accept-source-agreements + Exit $LASTEXITCODE + } + + "install" { + & $winget install --id $PackageName --accept-source-agreements --accept-package-agreements + Exit $LASTEXITCODE + } + + "uninstall" { + & $winget uninstall --id $PackageName --accept-source-agreements + Exit $LASTEXITCODE + } } From 58f483bfde8039e587eb27ccfc4a509e5c9a7051 Mon Sep 17 00:00:00 2001 From: dinger1986 Date: Sun, 24 May 2026 12:47:13 +0100 Subject: [PATCH 2/2] Fix typo in parameter options for WinGet script --- scripts/Win_WinGet_Manage_Apps.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/Win_WinGet_Manage_Apps.ps1 b/scripts/Win_WinGet_Manage_Apps.ps1 index e2080aea..04253dd1 100644 --- a/scripts/Win_WinGet_Manage_Apps.ps1 +++ b/scripts/Win_WinGet_Manage_Apps.ps1 @@ -4,7 +4,7 @@ .DESCRIPTION For installing packages using winget. .PARAMETER Mode - 4 options: install, uninstall, search or upgrade. + 4 options: install, uninstall,, update, show, search or upgrade. .PARAMETER PackageName Use this to specify which software to install eg: PackageName google.chrome .EXAMPLE