在命令行中使用 WinGet 安装和管理应用程序

WinGet的功能
WinGet是微软商店的命令行版,是微软2020年和和Windows Terminal上线的,功能如下所示:
PS C:\Users\U> winget
Windows 程序包管理器 v1.10.390
版权所有 (C) Microsoft Corporation。保留所有权利。
WinGet 命令行实用工具可从命令行安装应用程序和其他程序包。
使用情况: winget [<命令>] [<选项>]
下列命令有效:
install 安装给定的程序包
show 显示包的相关信息
source 管理程序包的来源
search 查找并显示程序包的基本信息
list 显示已安装的程序包
upgrade 显示并执行可用升级
uninstall 卸载给定的程序包
hash 哈希安装程序的帮助程序
validate 验证清单文件
settings 打开设置或设置管理员设置
features 显示实验性功能的状态
export 导出已安装程序包的列表
import 安装文件中的所有程序包
pin 管理包钉
configure 将系统配置为所需状态
download 从给定的程序包下载安装程序
repair 修复所选包
如需特定命令的更多详细信息,请向其传递帮助参数。 [-?]
...
WinGet源配置
WinGet源的当前配置可以用source命令通过以下方式查看:
PS C:\Users\U> winget source list
名称 参数 显式
-----------------------------------------------------------
msstore https://storeedgefd.dsx.mp.microsoft.com/v9.0 false
winget https://cdn.winget.microsoft.com/cache false
如果觉得微软官方源太慢,可以设置国内镜像源(如,USTC镜像),这一步需要管理员权限,否则会有以下提示:
PS C:\Users\U> winget source remove winget
需要具有管理员权限才能执行此命令。
正确方法是用sudo执行(V24H2及以上版本):
PS C:\Users\U> sudo winget
已在此计算机上禁用 Sudo。若要启用它,请转到“设置”应用中的 Developer Settings page
或者打开管理员终端:
PS C:\Users\U> winget source remove winget
正在删除源: winget...
██████████████████████████████ 100%
完成
如果,WinGet的版本较新,版本号>=1.8,运行:
PS C:\Users\U> winget source add winget https://mirrors.ustc.edu.cn/winget-source --trust-level trusted
正在添加源:
winget -> https://mirrors.ustc.edu.cn/winget-source
██████████████████████████████ 100%
完成
PS C:\Users\U> winget source list
名称 参数 显式
-----------------------------------------------------------
winget https://mirrors.ustc.edu.cn/winget-source false
msstore https://storeedgefd.dsx.mp.microsoft.com/v9.0 false
如果WinGet版本号<=1.7,则不需要--trust-level trusted选项。接下来先搜索一下试试:
PS C:\Users\U> winget search Python.Python.3
“msstore”源要求在使用前查看以下协议。
Terms of Transaction: https://aka.ms/microsoft-store-terms-of-transaction
源要求将当前计算机的 2 个字母的地理区域发送到后端服务才能正常工作,(例如"US")。
是否同意所有源协议条款?
[Y] 是 [N] 否: Y
名称 ID 版本 源
----------------------------------------------
Python 3.0 Python.Python.3.0 3.0.1 winget
Python 3.1 Python.Python.3.1 3.1.4 winget
Python 3.10 Python.Python.3.10 3.10.11 winget
Python 3.11 Python.Python.3.11 3.11.9 winget
Python 3.12 Python.Python.3.12 3.12.10 winget
Python 3.13 Python.Python.3.13 3.13.5 winget
Python 3.2 Python.Python.3.2 3.2.5 winget
Python 3.3 Python.Python.3.3 3.3.5 winget
Python 3.4 Python.Python.3.4 3.4.4 winget
Python 3.5 Python.Python.3.5 3.5.4 winget
Python 3.6 Python.Python.3.6 3.6.8 winget
Python 3.7 Python.Python.3.7 3.7.9 winget
Python 3.8 Python.Python.3.8 3.8.10 winget
Python 3.9 Python.Python.3.9 3.9.13 winget
第一次使用winget可能会有协议条款提示,选择是【Y】即可,能成功搜到说明源工作正常。接下来就可以愉快地安装软件了。安装方法为
PS C:\Users\U> winget install
例如,安装最新版的Python则运行:
PS C:\Users\U> winget install Python.Python.3.13
WinGet也可以安装微软商店(即:上文的msstore源)的软件,常用软件安装的方法见《使用winget快速安装常用软件》。最后,如果要恢复官方源,只需运行:
PS C:\Users\U> winget source reset winget
更多WinGet信息详见微软官网手册