Skip to content

Instantly share code, notes, and snippets.

@karaeren
Last active February 10, 2024 16:50
Show Gist options
  • Save karaeren/5b1ca6e523231e4cbdb9ee52d5dfccf4 to your computer and use it in GitHub Desktop.
Save karaeren/5b1ca6e523231e4cbdb9ee52d5dfccf4 to your computer and use it in GitHub Desktop.
Install multiple packages with winget (and install winget if does not exist).
# Create a file named "winget.txt" in the same directory as this script.
# Write exact names for packages on each line.
# Ex: filename: winget.txt
# Microsoft.Edge
# Google.Chrome
Write-Host "Checking winget..."
Try{
# Check if winget is already installed
$er = (invoke-expression "winget -v") 2>&1
if ($lastexitcode) {throw $er}
Write-Host "winget is already installed."
}
Catch{
# winget is not installed. Install it from the Github release
Write-Host "winget is not found, installing it right now."
$repo = "microsoft/winget-cli"
$releases = "https://api.github.com/repos/$repo/releases"
Write-Host "Determining latest release"
$json = Invoke-WebRequest $releases
$tag = ($json | ConvertFrom-Json)[0].tag_name
$file = ($json | ConvertFrom-Json)[0].assets[0].name
$download = "https://github.com/$repo/releases/download/$tag/$file"
$output = $PSScriptRoot + "\winget-latest.appxbundle"
Write-Host "Dowloading latest release"
Invoke-WebRequest -Uri $download -OutFile $output
Write-Host "Installing the package"
Add-AppxPackage -Path $output
}
Finally {
# Start installing the packages with winget
Get-Content .\winget.txt | ForEach-Object {
iex ("winget install -e " + $_)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment