GNU General Public License v3.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度技术分析:asheroto/winget-install

项目背景与痛点

Windows Package Manager(简称 Winget)作为 Windows 的内置包管理器,其安装流程一直是用户入门的一大痛点。由于官方文档提供的安装步骤较为复杂且缺少自动化脚本支持,很多用户在尝试安装 Winget 时感到困扰。特别是对于那些有严格依赖要求的环境,手动安装可能会导致疏漏。asheroto/winget-install 正是为了解决这些问题而诞生。

在 Windows 10 和 Windows 11 系统中,包管理器的引入简化了软件安装和管理的过程。asheroto/winget-install 提供了一种自动化安装 Winget 的方法,它利用 PowerShell 脚本自动处理所有准备工作,确保各种依赖项的存在并执行安全详尽的安装过程。这一项目对于那些想要快速在新机器上部署 Winget 的用户来说尤为重要,同时也减少了因为手动安装过程中可能出现的错误。

核心技术揭秘

asheroto/winget-install 项目的核心技术在于其对 PowerShell 脚本的高效利用。通过 PowerShell 脚本,项目可以在多种 Windows 环境中简化 Winget 的安装流程,消除其复杂性。下面图表展示了其核心流程。

graph TD A[用户调用脚本] --> B{"检查环境"} B -->|"通过"| C[下载 Winget] B -->|"失败"| D[显示错误] C --> E[安装依赖项] E --> F{"验证安装"} F -->|"成功"| G[Winget 安装完成] F -->|"失败"| H[显示安装失败原因]

项目中具体使用到了以下技术栈和设计模式:

  • PowerShell 脚本设计:通过编写功能完善的 PowerShell 脚本,实现自我检测环境、自动下载、自动配置和依赖管理等功能。
  • 模块化设计:将安装过程拆分成多个步骤,每个步骤可以独立运行,便于维护且易于扩展。
  • 错误处理机制:在安装流程中,通过捕获异常并动态调整执行步骤,确保在遇到错误时不会无故中断,从而提高可靠性。
  • 自动化依赖安装:通过在安装脚本中对依赖项的自动检查,确保用户环境具备正确依赖项,避免手动安装错误。

功能亮点与差异

相较于其他同类竞品,asheroto/winget-install 的独特之处在于其的自动化安装与环境适配能力。该脚本不仅确保了 Winget 的顺畅安装,还在多种 Windows 版本上进行了兼容性验证(包括 Windows 10/11 以及 Server 2019/2022),减少了用户手动查找和安装依赖项的麻烦。

另外,项目开源的特点也使得其具有高度的透明性和社区参与性,用户可以自行验证脚本逻辑,减少对第三方安装工具的依赖和信任成本,也方便用户同步贡献改进或新增功能。

应用场景与落地建议

asheroto/winget-install 最适用于面向 IT 基础设施自动化的场景,例如:

  • 企业级环境部署:在大规模 Windows Server 机群中自动化安装和配置 Winget,简化维护工作。
  • 新机器部署流程:集成此脚本到新机器部署流程中,快速、批量地自动配置 Winget。
  • 开发环境重建:开发团队在构建或迁移开发环境时,可利用此脚本快速部署所需的软件环境。

然而在生产环境中使用,需要注意一些细节:

  • 权限管理:请确保运行脚本时具有足够的权限。
  • 安全性检查:推荐在正式环境运行前,在隔离环境中先行测试。
  • 用户反馈:鼓励用户反馈脚本执行过程中遇到的问题,以便持续改进。

综合评价

asheroto/winget-install 是一个简洁而高效的脚本项目,核心亮点在于其自动化安装和高度可维护性,在应对多样化的 Windows 环境时具有很高的灵活性。虽然代码本身并不复杂,但因为它实现了复杂的依赖管理和自我检测机制,可以用最小的代码量实现最理想的效果。

不过,该项目目前存在的主要缺陷在于对某些特殊环境的兼容性测试不够全面,存在潜在的风险点。此外,依赖文件升级或补丁部署时,脚本需要及时更新以确保继续顺利运行。

总的来说,在快速部署和自动化时代,asheroto/winget-install 无疑是一个值得推荐的脚本,对于希望减少安装脚本维护工作量的用户来说,是一件利器。

关于

GitHub 原始描述(英文)

Install WinGet using PowerShell! Prerequisites automatically installed. Works on Windows 10/11 and Server 2019/2022.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
794
价值分
412
活跃状态
活跃

语言占比

PowerShell

Release

README

winget-install screenshot

PowerShell Gallery Downloads GitHub Downloads - All Releases Release GitHub Release Date - Published_At

GitHub Sponsor Ko-Fi Button Buy Me a Coffee

Install WinGet from PowerShell

WinGet, a command line package manager, doesn’t have a command line installer? 🤣 Now it does! 😊

[!NOTE]
What’s New

  • Fixed dependencies that were silently skipped instead of installed on Windows Server 2019 and 2022.
  • Fixed -PortableInstallMethod also running the standard install on Windows 10 and 11.
  • Fixed PATH handling so entries such as %LOCALAPPDATA% are stored as expandable strings and existing entries are no longer flattened.
  • Hardened portable extraction against path traversal and manifest parsing against external entities.
  • Added a Pester test suite under tests/.
  • Added support for GitHub API tokens (-GHtoken) to improve reliability when checking for the latest WinGet version or when encountering GitHub API rate limits.
  • Added the ability to specify a specific WinGet version using -WingetVersion.
    Note: This only works with -AlternateInstallMethod or -PortableInstallMethod. The alternate method is used automatically on Windows Server 2019, Windows Server 2022, and when running as SYSTEM.

Table of Contents

Requirements

  • Requires PowerShell running with Administrator privileges - OR - execution under the SYSTEM context
  • Compatible with:
    • Windows 10 (Version 1809 or higher)
    • Windows 11
    • Server 2019/2022/2025
    • Windows Sandbox
    • Server Core (non desktop experience) - still under beta testing, see issue #53 for details
  • Not compatible with:
    • Server 2016 or lower (WinGet not supported)

Features

  • Installs WinGet directly from PowerShell
  • Always fetches the latest WinGet version
  • Automatically verifies OS compatibility
  • Determines and installs the appropriate prerequisites based on OS version
  • Supports x86/x64 and arm/arm64 architectures
  • Allows bypassing of existing WinGet installation verification through -Force parameter or $Force session variable
  • Supports irm/iex one-line command using short URL
  • Supports automatically relaunching in conhost and ending active processes associated with WinGet that could interfere with the installation
  • Code is hosted on PowerShell Gallery
  • Supports running under the SYSTEM context

Script Functionality

  • Identifies processor architecture to decide which prerequisites are needed (x86/x64 or arm/arm64)
  • Checks Windows OS version for compatibility (Windows 10, Windows 11, Server 2019/2022/2025)
  • If Windows 10, verifies release ID for compatibility (must be 1809 or newer)
  • Windows 10, Windows 11, and Server 2025 (standard method)
    • Installs the NuGet package provider for PowerShell Gallery (if not already present)
    • Installs the Microsoft.WinGet.Client from PowerShell Gallery
    • Runs Repair-WinGetPackageManager -AllUsers which actually installs WinGet and its dependencies, fixing any issues along the way
  • Server 2019, Server 2022, SYSTEM context, or -AlternateInstallMethod (alternate method)
    • Uses the UI.Xaml and VCLibs as recommended by Microsoft
    • The WinGet-cli license is downloaded using the latest version from GitHub
    • Installs Visual C++ Redistributable if version 14 or higher is not already installed
    • WinGet-cli is then installed using the latest version from GitHub
  • Server Core or -PortableInstallMethod (portable method, beta)
    • Extracts WinGet and its dependencies to Program Files\Microsoft\winget without AppX registration
    • Adjust access rights & PATH environment variable
  • Runs command registration if the WinGet command is not detected at the end of installation

Setup

This is the recommended method, because it always gets the public release that has been tested, it’s easy to remember, and supports all parameters.

Open PowerShell as Administrator and type

Install-Script winget-install -Force

Follow the prompts to complete the installation (you can tap A to accept all prompts or Y to select them individually.

Note: -Force is optional but recommended, as it will force the script to update if it is outdated. If you do not use -Force, it will not overwrite the script if outdated.

Usage
winget-install

If WinGet is already installed, you can use the -Force parameter to force the script to run anyway.

The script is published on PowerShell Gallery under winget-install.

Method 2 - One Line Command (Runs Immediately)

The URL asheroto.com/winget always redirects to the latest release of the script.

If you just need to run the basic script without any parameters, you can use the following one-line command:

Option A: asheroto.com short URL
irm asheroto.com/winget | iex

Due to the nature of how PowerShell works, you won’t be able to use any parameters like -Force with this command line. You can set the global variable $Force to $true and the script should pick up on it (more info), but if for some reason that doesn’t work or you absolutely need to use a one-line command with parameters, you can use the following:

&([ScriptBlock]::Create((irm asheroto.com/winget))) -Force
Option B: WinGet.pro short URL

To make it easier to remember, you can also use the URL WinGet.pro which redirects to the same URL. This URL is provided courtesy of Omaha Consulting.

irm winget.pro | iex

Due to the nature of how PowerShell works, you won’t be able to use any parameters like -Force with this command line. You can set the global variable $Force to $true and the script should pick up on it (more info), but if for some reason that doesn’t work or you absolutely need to use a one-line command with parameters, you can use the following:

&([ScriptBlock]::Create((irm winget.pro))) -Force
Option C: direct release URL

Alternatively, you can of course use the latest release URL directly:

irm https://github.com/asheroto/winget-install/releases/latest/download/winget-install.ps1 | iex

Method 3 - Download Locally and Run

As a more conventional approach, download the latest winget-install.ps1 from Releases, then run the script as follows:

.\winget-install.ps1
Note 注意

If PowerShell refuses to run the downloaded file, run Unblock-File .\winget-install.ps1 once, or launch it with powershell -ExecutionPolicy Bypass -File .\winget-install.ps1.

You can use the -Force or -ForceClose parameters if needed, or use $Force = $true and $ForceClose = $true global session variables if preferred.

Tip 提示

If for some reason your PowerShell window closes at the end of the script and you don’t want it to, or don’t want your other scripts to be interrupted, you can wrap the command in a powershell "COMMAND HERE". For example, powershell "irm asheroto.com/winget | iex".

[!NOTE] On Windows 11 IoT, you may need to run the following command. Unlike other editions that may prompt about Execution Policy, IoT might not: powershell -ep B "irm asheroto.com/winget | iex"

Parameters

No parameters are required to run the script, but there are some optional parameters to use if needed.

Parameter Description
-Debug Enables debug mode, showing additional information for troubleshooting.
-Force Ensures the installation of WinGet and its dependencies, even if they are already present.
-ForceClose If Windows Terminal has trouble installing WinGet, use this parameter to relaunch the script in conhost.exe and automatically end any active processes associated with WinGet that could interfere with the installation.
-AlternateInstallMethod Installs WinGet by downloading the dependencies, license, and package from the winget-cli GitHub repository instead of using Repair-WinGetPackageManager. Used automatically on Windows Server 2019, Windows Server 2022, and when running as SYSTEM. Try this if the primary method fails.
-PortableInstallMethod Extracts WinGet and its dependencies to Program Files\Microsoft\winget without AppX registration. Used automatically on Windows Server Core. Beta, see issue #53.
-WingetVersion Specifies a particular version of WinGet to install. Only supported with -AlternateInstallMethod or -PortableInstallMethod; otherwise, the latest version will always be installed.
-GHtoken Optional GitHub API token to increase API rate limits or access private repositories when downloading WinGet releases.
-CheckForUpdate Checks if there is an update available for the script.
-Wait By default, the script exits immediately after completion. Although this is not supposed to close the PowerShell window, sometimes it still occurs. Use this parameter to pause the script for several seconds before exiting to observe the output.
-NoExit By default, the script exits immediately after completion. Although this is not supposed to close the PowerShell window, sometimes it still occurs. Use this parameter to pause the script indefinitely after execution; pressing Enter will exit the script.
-UpdateSelf Updates the script to the latest version.
-Version Displays the version of the script.
-Help Displays the full help information for the script.

Example Parameters Usage

winget-install -Force

Global Variables

Global variables are optional and are only needed if you don’t want to use parameters. They can be set before running the script, or you can set them in your PowerShell profile to always use them.

Variable Description
$Debug Enables debug mode, which shows additional information for debugging.
$Force Ensures installation of WinGet and its dependencies, even if already present.
$ForceClose Windows Terminal sometimes has trouble installing WinGet; run the script with the -ForceClose parameter to relaunch the script in conhost.exe and automatically end active processes associated with WinGet that could interfere with the installation

Example Global Variables Usage

$Force = $true
winget-install

Troubleshooting

Note 注意

Before releasing a new version, the script is tested on a clean install of Windows 10, Windows 11, Server 2019, Server 2022.

  • If you run into an issue, please ensure your system is compatible & fully updated.
  • If you see an error message when installing, try using the -AlternateInstallMethod parameter.
  • Sometimes PowerShell closes the window before you can read the output. To prevent this, you can use the -Wait parameter to pause the script for a few seconds or the -NoExit parameter to keep the window open indefinitely. Improving script exit functionality is on our TODO list.
  • Try running winget-install again, sometimes the script will fail due to a temporary issue with the prerequisite server URLs.
  • Try using the -Debug parameters to see if it provides any additional information.
  • If winget is unable to run at some point in the future, or if you suspect an issue with the PATH environment variable, run the script again with the -Force parameter.
  • If you’re getting a resource in use error message, run the script again with the -ForceClose parameter.
  • Try installing WinGet manually to see if the issue exists with WinGet itself.
  • If the issue occurs when installing WinGet manually, please open an issue on the WinGet-cli repo (unrelated to this script).
  • Check the WinGet-cli Troubleshooting Guide.
  • If the problem only occurs when using this script, please open an issue here.

Contributing

If you’d like to help develop this project: fork the repo, edit the code, then submit a pull request. 😊

评论

暂无评论