3

I'm trying to write a PowerShell script to setup windows dev machines. I want to use winget but I don't see any easy way just to install winget using the commandline. You have to either use the windows store or download the msxibundle from github.

Invoke-WebRequest -Uri https://github.com/microsoft/winget-cli/releases/download/v1.3.2691/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle -OutFile .\MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle

Both of these require user's interaction instead of just running the script and walking away. Is there a better way? Thanks.

Donald Herman
  • 314
  • 1
  • 5
  • 15
  • It's the installer, provided by the vendor that must allow options for silent installs, not PowerShell or other scripting languages. Languages are just used to start the installer and you pass the proper switches/install parameters. [What did you search for regarding ```msxibundle``` installer features](https://duckduckgo.com/?q=%27winget+msxibundle++silent%27&t=h_&ia=web)? – postanote Oct 22 '22 at 18:54
  • @postanote I couldn't find anything using google. Start-Automating answer works great. – Donald Herman Oct 31 '22 at 05:41

6 Answers6

8

I devised a somewhat different approach, which installs the latest version:

# get latest download url
$URL = "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
$URL = (Invoke-WebRequest -Uri $URL).Content | ConvertFrom-Json |
        Select-Object -ExpandProperty "assets" |
        Where-Object "browser_download_url" -Match '.msixbundle' |
        Select-Object -ExpandProperty "browser_download_url"

# download
Invoke-WebRequest -Uri $URL -OutFile "Setup.msix" -UseBasicParsing

# install
Add-AppxPackage -Path "Setup.msix"

# delete file
Remove-Item "Setup.msix"
Vladan
  • 725
  • 8
  • 13
  • 1
    This seems to rely on Internet Explorer's infrastructure for request handling. Returns an error if IE is not initialized or missing. Adding `-UseBasicParsing` to `Invoke-WebRequest` makes it work. There's a nice progress bar in the console as a bonus. – Andy Jun 10 '23 at 08:05
  • I wrote up a fixed command with other error cases at https://winget.pro/winget-install-powershell/. – Michael Herrmann Jul 24 '23 at 17:04
3

Using Windows PowerShell, not PowerShell Core, there's a command to help: Add-AppXPackage:

Add-AppXPackage -Path .\MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle

Should allow you to install the package.

Hope this helps

Start-Automating
  • 8,067
  • 2
  • 28
  • 47
3

Here is a script for that directly from Microsoft. Script can also be used to install winget into Windows sandbox.

Original page

$progressPreference = 'silentlyContinue'
$latestWingetMsixBundleUri = $(Invoke-RestMethod https://api.github.com/repos/microsoft/winget-cli/releases/latest).assets.browser_download_url | Where-Object {$_.EndsWith(".msixbundle")}
$latestWingetMsixBundle = $latestWingetMsixBundleUri.Split("/")[-1]
Write-Information "Downloading winget to artifacts directory..."
Invoke-WebRequest -Uri $latestWingetMsixBundleUri -OutFile "./$latestWingetMsixBundle"
Invoke-WebRequest -Uri https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx -OutFile Microsoft.VCLibs.x64.14.00.Desktop.appx
Add-AppxPackage Microsoft.VCLibs.x64.14.00.Desktop.appx
Add-AppxPackage $latestWingetMsixBundle
user16217248
  • 3,119
  • 19
  • 19
  • 37
0
Install-Module -Name Microsoft.WinGet.Client
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 3
    Hello, and welcome! This is an interesting response, can you provide a description of what it does? Far as I can tell, it automatically installs [NuGet](https://learn.microsoft.com/en-us/nuget/what-is-nuget), then prompts to install an untrusted repository called `PSGallery`. I didn't end up with winget in my path after trying it. – blee Aug 08 '23 at 21:38
0

I want to point out that the code linked above and provided by Microsoft is not accurate. Updated with correct PowerShell:

 $progressPreference = 'silentlyContinue'
        $latestWingetMsixBundleUri = $( Invoke-RestMethod https://api.github.com/repos/microsoft/winget-cli/releases/latest ).assets.browser_download_url | Where-Object { $_.EndsWith( ".msixbundle" ) }
        $latestWingetMsixBundle = $latestWingetMsixBundleUri.Split("/")[-1]
        Write-Information "Downloading winget to artifacts directory..."
        Invoke-WebRequest -Uri $latestWingetMsixBundleUri -OutFile "./$latestWingetMsixBundle" 
Add-AppxPackage $latestWingetMsixBundle
SPGrinch
  • 1
  • 1
0

This worked perfectly for me

# get latest download url
$URL = "https://api.github.com/repos/microsoft/winget-cli/releases/latest"
$URL = (Invoke-WebRequest -Uri $URL).Content | ConvertFrom-Json |
        Select-Object -ExpandProperty "assets" |
        Where-Object "browser_download_url" -Match '.msixbundle' |
        Select-Object -ExpandProperty "browser_download_url"
$LicenseFileURL = 'https://github.com/microsoft/winget-cli/releases/download/v1.2.10271/b0a0692da1034339b76dce1c298a1e42_License1.xml'

# download
Invoke-WebRequest -Uri $URL -OutFile "Setup.msix" -UseBasicParsing
Invoke-WebRequest -Uri $LicenseFileURL -OutFile  'license.xml' 

# install
#Add-AppxPackage -Path "Setup.msix" -LicensePath .\license.xml
Add-AppxProvisionedPackage -PackagePath "Setup.msix" -LicensePath 'license.xml' -online 

# delete file
Remove-Item "Setup.msix"