通过命令行(powershell)安装winget

Don*_*man 11 windows powershell windows-10 winget

我正在尝试编写一个 PowerShell 脚本来设置 Windows 开发机器。我想使用 winget,但我没有看到任何使用命令行安装 winget 的简单方法。您必须使用 Windows 应用商店或从 github 下载 msxibundle。

Invoke-WebRequest -Uri https://github.com/microsoft/winget-cli/releases/download/v1.3.2691/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle -OutFile .\MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle
Run Code Online (Sandbox Code Playgroud)

这两者都需要用户的交互,而不是仅仅运行脚本然后走开。有没有更好的办法?谢谢。

Vla*_*dan 10

我设计了一种稍微不同的方法,它安装最新版本:

# 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"
Run Code Online (Sandbox Code Playgroud)

  • 我在 https://winget.pro/winget-install-powershell/ 上编写了一个包含其他错误情况的固定命令。 (3认同)
  • 这似乎依赖于 Internet Explorer 的基础结构来处理请求。如果 IE 未初始化或丢失,则返回错误。将“-UseBasicParsing”添加到“Invoke-WebRequest”使其工作。作为奖励,控制台中有一个漂亮的进度条。 (2认同)

小智 7

这是直接来自 Microsoft 的脚本。脚本还可用于将 winget 安装到 Windows 沙箱中。

原页

$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
Run Code Online (Sandbox Code Playgroud)


Sta*_*ing 5

将 msixbundle 下载到 后.\MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle,您可以使用 Windows PowerShell(而不是 PowerShell Core)安装它,有一个命令可以提供帮助Add-AppXPackage::

Add-AppXPackage -Path .\MicrosoftDesktopAppInstaller_8wekyb3d8bbwe.msixbundle
Run Code Online (Sandbox Code Playgroud)

应该允许您安装下载的软件包。

希望这可以帮助