Skip to content

Instantly share code, notes, and snippets.

@jfalava
Last active March 6, 2024 14:24
Show Gist options
  • Save jfalava/7ed352478721bcf10ff3da1cae6a6623 to your computer and use it in GitHub Desktop.
Save jfalava/7ed352478721bcf10ff3da1cae6a6623 to your computer and use it in GitHub Desktop.

PowerShell script that creates another script by parsing your winget list output and creating a new script file with all the packages available to be installed with winget and executable as it is.`

Note

If this is your first time using winget you, for the script to work, need to accept its terms of service.
You can do so by executing any winget command before executing this PowerShell script.

# Get the list of installed packages via winget
$installedPackagesOutput = winget list

# Check if output is empty
if (-not $installedPackagesOutput) {
    Write-Host "No output from 'winget list'. Please ensure Winget is installed and working correctly."
    exit
} else {
    Write-Host "Successfully retrieved the list of installed packages."
}

# Parse the output into an array, skipping the first two lines (headers)
$installedPackages = $installedPackagesOutput -split "`r`n" | Select-Object -Skip 2

# Initialize an empty array to hold the install commands
$installScriptContent = @()

# Define a regex pattern for valid package IDs
$validPackageIdPattern = '^[A-Za-z0-9\.\-_]+$'

# Go through each installed package and extract the Id for "winget" or "msstore"
foreach ($package in $installedPackages) {
    if (-not [string]::IsNullOrWhiteSpace($package)) {
        # Use regex to split the line by multiple spaces, then take the necessary columns
        $packageDetails = $package -split '\s\s+'
        if ($packageDetails.Count -ge 4) {
            $origen = $packageDetails[3]
            if ($origen -eq 'winget' -or $origen -eq 'msstore') {
                $packageId = $packageDetails[1]
                # Validate the package ID
                if ($packageId -match $validPackageIdPattern) {
                    $installCommand = "winget install $packageId --accept-package-agreements --accept-source-agreements"
                    $installScriptContent += $installCommand
                } else {
                    Write-Host "Skipping invalid package: ID = $packageId, Origen = $origen"
                }
            }
        }
    }
}

# Define the path for the new install script
$installScriptPath = "wingetListOutput-installScript.ps1"

# Check if there are any install commands to write to the script
if ($installScriptContent) {
    $installScriptContent | Out-File -FilePath $installScriptPath -Encoding UTF8
    Write-Host "Winget install script has been created at: $installScriptPath"
} else {
    Write-Host "No install commands generated. There might be an issue with the list of installed packages for 'winget' or 'msstore'."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment