Last active
October 1, 2023 12:29
-
-
Save Satal/3d3dc8834f8ecfafd92e36fbd648c658 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install in C:\users\[user]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 | |
# You may need to run `Set-ExecutionPolicy RemoteSigned` if you get an error about running | |
function wingetlist { | |
# Based on the script provided at https://gist.github.com/alkampfergit/2f662c07df0ca379c8e8e65e588c687b | |
# Create a collection of strings for the ids of software that should be ignored | |
$ignoreList = @( | |
'Google.Chrome', | |
'JetBrains.PHPStorm' | |
) | |
class Software { | |
[string]$Name | |
[string]$Id | |
[string]$Version | |
[string]$AvailableVersion | |
} | |
$upgradeResult = winget upgrade | Out-String | |
$lines = $upgradeResult.Split([Environment]::NewLine) | |
# Find the line that starts with Name, it contains the header | |
$fl = 0 | |
while (-not $lines[$fl].StartsWith("Name")) | |
{ | |
$fl++ | |
} | |
# Line $i has the header, we can find char where we find ID and Version | |
$idStart = $lines[$fl].IndexOf("Id") | |
$versionStart = $lines[$fl].IndexOf("Version") | |
$availableStart = $lines[$fl].IndexOf("Available") | |
$sourceStart = $lines[$fl].IndexOf("Source") | |
# Now cycle in real package and split accordingly | |
$upgradeList = @() | |
For ($i = $fl + 1; $i -le $lines.Length; $i++) | |
{ | |
$line = $lines[$i] | |
if ($line.Length -gt ($availableStart + 1) -and -not $line.StartsWith('-')) | |
{ | |
$name = $line.Substring(0, $idStart).TrimEnd() | |
$id = $line.Substring($idStart, $versionStart - $idStart).TrimEnd() | |
$version = $line.Substring($versionStart, $availableStart - $versionStart).TrimEnd() | |
$available = $line.Substring($availableStart, $sourceStart - $availableStart).TrimEnd() | |
$software = [Software]::new() | |
$software.Name = $name; | |
$software.Id = $id; | |
$software.Version = $version | |
$software.AvailableVersion = $available; | |
# If $id is not in $ignoreList, add it to $upgradeList | |
if ($ignoreList -notcontains $id) | |
{ | |
$upgradeList += $software | |
} | |
} | |
} | |
$upgradeList | Format-Table | |
$ids = $upgradeList | Select-Object -ExpandProperty Id | |
# Create a copy of the ids list removing any items that have a space in them | |
$ids = $ids | Where-Object { $_ -notlike '* *' } | |
$ids = $ids -join "', '" | |
# print ids | |
Write-Host "@('$ids') | % { winget upgrade `$_ }" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment