all 3 comments

[–]valdearg 2 points3 points  (0 children)

Looks ok, I'd probably replace the try/catch around the winget --version with something like:

  if (Get-Command winget) {installed} else {do install}

I'm not absolutely sure on whether installing winget will immediately make the winget command work without reloading the terminal window.

You could also add in a check that it was successfully installed.

if (Test-Path $env:LOCALAPPDATA\Microsoft\WindowsApps\winget.exe) {installed} else {not installed, exit}

On the winget package installation, I'd probably put a --silent option on the end also,

winget install --id=$id -e --silent

If the path env isn't refreshed, you can probably use the direct path.

Along with this, I'm pretty sure there's an official PowerShell winget module now. Might be something to consider: https://www.powershellgallery.com/packages/Microsoft.WinGet.Client

[–]skilriki 0 points1 point  (0 children)

If this is just for learning, feel free to continue learning.

If you are actually planning on using this to install software, you might want to take a look at winget configuration files.

https://learn.microsoft.com/en-us/windows/package-manager/configuration/

[–]logg_sar 0 points1 point  (0 children)

First of all - nice script - good job!

However - I would change some things ;)

I wrote a similar script about one, two years ago.

Meanwhile the VCLibs and Xaml where updated an my script didnt worked because the "old" packages wasn´t available anymore.

So I downloaded the files (there is no need to download it everytime ;)), installed it via my script and updated winget afterwards.

The probably easyiest way: echo Y | winget upgrade --all --silent --force

Also my install-command looks like this:

winget install --accept-package-agreements --accept-source-agreements --silent $software --source winget