Loading... ## 前言 记录下Windows系统下类似Linux平台yum/apt等的命令行软件包管理工具。 ## WinGet --- 项目地址:https://github.com/microsoft/winget-cli WinGet全称为"windows package manager", 是微软官方推出基于命令行的软件包管理工具, 目前GitHub上最新稳定版本v1.8.1911。使用它可以非常方便地直接通过微软官方对软件进行搜索、下载、安装、升级、卸载(删除)、配置等操作,快速部署/管理我们的环境。 ![image.png](https://zoe.red/usr/uploads/2024/09/1657239247.png) 官方文档: https://learn.microsoft.com/zh-cn/windows/package-manager/winget/ 需要注意: `winget`命令行工具仅在 Windows 10 1709(版本 16299)或更高版本上受支持。可以直接[从 Microsoft Store 获取应用安装程序](https://www.microsoft.com/p/app-installer/9nblggh4nns1#activetab=pivot:overviewtab)(**点此链接会跳转到应用商店安装**)。 如果已安装,请确保已将其更新为最新版本。 * Windows Server 2019不支持,因为 Microsoft Store 不可用,更新的依赖项也不可用。 安装完毕后, 打开powershell或terminal工具, 输入`winget -v`, 验证安装版本: ![image.png](https://zoe.red/usr/uploads/2024/09/1362792986.png) 常用命令示例脚本 * `winget`有两个默认的软件源: 第一个是微软应用商店中的 `msstore`(每个人都可以免费下载评级为 "e "的应用程序)。第二个是[社区软件仓库](https://github.com/microsoft/winget-pkgs)。 ```powershell # 查找 winget search <appname> # 安装 winget install <appname> # 示例安装 terminal powertoy vscode winget install Microsoft.WindowsTerminal Microsoft.PowerToys Microsoft.VisualStudioCode # 升级 winget upgrade --all # 替换国内源: 以中科大源为例 winget source remove winget winget source add winget https://mirrors.ustc.edu.cn/winget-source # 重置官方源 winget source reset winget ``` 更多参数用法参见帮助信息 ```powershell PS C:\Users\*****> winget --help Windows 程序包管理器 v1.8.1911 版权所有 (C) Microsoft Corporation。保留所有权利。 WinGet 命令行实用工具可从命令行安装应用程序和其他程序包。 使用情况: winget [<命令>] [<选项>] 下列命令有效: install 安装给定的程序包 show 显示包的相关信息 source 管理程序包的来源 search 查找并显示程序包的基本信息 list 显示已安装的程序包 upgrade 显示并执行可用升级 uninstall 卸载给定的程序包 hash 哈希安装程序的帮助程序 validate 验证清单文件 settings 打开设置或设置管理员设置 features 显示实验性功能的状态 export 导出已安装程序包的列表 import 安装文件中的所有程序包 pin 管理包钉 configure 将系统配置为所需状态 download 从给定的程序包下载安装程序 repair 修复所选包 如需特定命令的更多详细信息,请向其传递帮助参数。 [-?] 下列选项可用: -v,--version 显示工具的版本 --info 显示工具的常规信息 -?,--help 显示选定命令的帮助信息 --wait 提示用户在退出前按任意键 --logs,--open-logs 打开默认日志位置 --verbose,--verbose-logs 启用 WinGet 的详细日志记录 --nowarn,--ignore-warnings 禁止显示警告输出 --disable-interactivity 禁用交互式提示 --proxy 设置要用于此执行的代理 --no-proxy 禁止对此执行使用代理 ``` <span style='color:red'>注意: 在使用winget工具进行查找/安装/升级/卸载软件的操作过程中, 需要额外注意管理员权限可能导致的问题.</span> **扩展: 非管理员模式的powershell中如何类似Linux中sudo临时提升权限** --- * 方案: 使用gsudo工具, 输出如图 ```powershell # 1. 安装gsudo PS C:\Users\***> winget install gsudo 已找到 gsudo [gerardog.gsudo] 版本 2.5.1 此应用程序由其所有者授权给你。 Microsoft 对第三方程序包概不负责,也不向第三方程序包授予任何许可证。 正在下载 https://github.com/gerardog/gsudo/releases/download/v2.5.1/gsudo.setup.x64.msi ██████████████████████████████ 2.25 MB / 2.25 MB 已成功验证安装程序哈希 # 2设置别名sudo # gsudo看着还有些别扭的话,可以给它设置别名sudo # 2.1 管理员权限启动powershell PS C:\Users\***> gsudo powershell Windows PowerShell 版权所有 (C) Microsoft Corporation。保留所有权利。 尝试新的跨平台 PowerShell https://aka.ms/pscore6 # 2.2 设置别名 PS C:\Users\***> Set-Alias 'sudo' 'gsudo' ``` 至此配置完毕, 可以使用sudo 启动管理员权限的新powershell或者其他命令了 * 直接执行`sudo`或者执行`sudo powershell`均可以切换到`管理员模: Windows PowerShell`; 之后执行`exit`回车会推出管理员模式 ![image.png](https://zoe.red/usr/uploads/2024/09/573864896.png) ## Chocolatey --- 项目地址: https://github.com/chocolatey/choco 官网地址:https://chocolatey.org Chocolatey是一款专为Windows系统开发的、基于`NuGet`的包管理器工具,简称为`choco`, 开发者按需可以快速安装应用程序和工具。 **环境安装: 方式1(推荐)** ```powershell # 官方教程文档: https://docs.chocolatey.org/en-us/choco/setup/#more-install-options # 管理员权限在powershell执行 # 安装完毕后, 根据提示新开一个powershell, 验证choco是否安装成功 Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) # 重开一个powershell后 PS C:\Windows\system32> choco -v 2.3.0 # 升级自身, 形式类似python pip升级自身 PS C:\Windows\system32> choco upgrade chocolatey Chocolatey v2.3.0 Upgrading the following packages: chocolatey By upgrading, you accept licenses for the packages. chocolatey v2.3.0 is the latest version available based on your source(s). Chocolatey upgraded 0/1 packages. See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log). Enjoy using Chocolatey? Explore more amazing features to take your experience to the next level at https://chocolatey.org/compare ``` *扩展: 卸载choco* * 清理环境变量中PATH内容以及``安装文件 ![image.png](https://zoe.red/usr/uploads/2024/09/1133672716.png) --- **环境安装: 方式2** * 安装nuget, 之后基于nuget安装chocolatey ```powershell # 官方教程文档: https://community.chocolatey.org/courses/installation/installing?method=install-using-nugetexe-from-powershell # nuget安装配置 # 方式1: 官网下载nuget.exe配置到系统环境变量PATH中 # 下载页面, 如下图: https://www.nuget.org/downloads # https://dist.nuget.org/win-x86-commandline/latest/nuget.exe # 方式2(推荐): winget 直接安装nuget # 在terminal中执行, 输出见下图 winget install -e --id Microsoft.NuGet # 先切换到管理员权限执行 sudo powershell # 开始安装 # 预览版: nuget install chocolatey -pre # 稳定版下载 nuget install chocolatey # 进入到nuget下对应的下载目录, 执行安装 # 位置: C:\Users\***\.nuget\packages\chocolatey\2.3.0\tools # 执行安装 # 此处可能遇到权限策略问题, 根据输出提示, 打开在线地址 # 提示连接: https:/go.microsoft.com/fwlink/?LinkID=135170 # 修改当前用户权限策略: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # 之后在重新执行 .\chocolateyInstall.ps1 # 小结: # 安装信息同上手动卸载: # 文件目录: C:\ProgramData\chocolatey # 环境变量: PATH: C:\ProgramData\chocolatey\bin ``` nuget安装chocolatey详细输出: [nuget_install_chocho_detail.txt](https://zoe.red/usr/uploads/2024/09/1517883356.txt) nuget官方下载页面 ![image.png](https://zoe.red/usr/uploads/2024/09/2357842108.png) winget安装nuget ![image.png](https://zoe.red/usr/uploads/2024/09/3655810228.png) choco更多参数命令用法 ```bash PS C:\Users\*******> choco --help This is a listing of all of the different things you can pass to choco. Options and Switches -v, --version Version - Prints out the Chocolatey version. Commands * apikey - retrieves, saves or deletes an API key for a particular source * cache - Manage the local HTTP caches used to store queries (v2.1.0+) * config - Retrieve and configure config file settings * export - exports list of currently installed packages * feature - view and configure choco features * features - view and configure choco features (alias for feature) * find - searches remote packages (alias for search) * help - displays top level help information for choco * info - retrieves package information. Shorthand for choco search pkgname --exact --verbose * install - installs packages using configured sources * list - lists local packages * new - creates template files for creating a new Chocolatey package * outdated - retrieves information about packages that are outdated. Similar to upgrade all --noop * pack - packages nuspec, scripts, and other Chocolatey package resources into a nupkg file * pin - suppress upgrades for a package * push - pushes a compiled nupkg to a source * rule - view or list implemented package rules (v2.3.0+) * search - searches remote packages * setapikey - retrieves, saves or deletes an API key for a particular source (alias for apikey) * source - view and configure default sources * sources - view and configure default sources (alias for source) * template - get information about installed templates * templates - get information about installed templates (alias for template) * uninstall - uninstalls a package * unpackself - [DEPRECATED] will be removed in v3.0.0 - re-installs Chocolatey base files * upgrade - upgrades packages from various sources ``` 在线软件包查看/搜索页面: https://community.chocolatey.org/packages * 示例: 在搜索栏输入JDK8, 来到对应的页面, 复制需要的安装/更新/卸载命令即可 ![image.png](https://zoe.red/usr/uploads/2024/09/2949444537.png) 执行安装JDK命令 * 自动安装好的JDK包, 在系统的环境变量中自动帮我们设置好了`JAVA_HOME`和`PATH`对应的变量 ```powershell PS C:\Users\*******> choco install jdk8 Chocolatey v2.3.0 Installing the following packages: jdk8 By installing, you accept licenses for the packages. Downloading package from source 'https://community.chocolatey.org/api/v2/' Progress: Downloading jdk8 8.0.211... 100% jdk8 v8.0.211 [Approved] jdk8 package files install completed. Performing other installation steps. The package jdk8 wants to run 'chocolateyInstall.ps1'. Note: If you don't run this script, the installation will fail. Note: To confirm automatically next time, use '-y' or consider: choco feature enable -n allowGlobalConfirmation Do you want to run the script?([Y]es/[A]ll - yes to all/[N]o/[P]rint): y Downloading JDK from https://javadl.oracle.com/webapps/download/GetFile/1.8.0_211-b12/478a62b7d4e34b78b671c754eaaf38ab/windows-i586/jdk-8u211-windows-x64.exe Installing jdk8... jdk8 has been installed. PATH environment variable does not have C:\Program Files\Java\jdk1.8.0_211\bin in it. Adding... jdk8 may be able to be automatically uninstalled. Environment Vars (like PATH) have changed. Close/reopen your shell to see the changes (or in powershell/cmd.exe just type `refreshenv`). The install of jdk8 was successful. Deployed to 'C:\Program Files\Java\jdk1.8.0_211\' Chocolatey installed 1/1 packages. See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log). # 重新打开一个终端, 验证是否生效 PS C:\Users\*******> java -version java version "1.8.0_211" Java(TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode) ``` 执行安装iperf3: 测试输出详情见 [choco_install_iperf3.txt](https://zoe.red/usr/uploads/2024/09/4233211660.txt) 开发常用工具安装(管理员权限): ```bash # vscode choco install vscode.install -y # git choco install git.install -y # filezilla, 远程文件管理 choco install filezilla -y # 7zip choco install 7zip -y # vim or neovim choco install vim -y # 之后可以直接执行vim choco install neovim -y # 之后需要新开pwsh/cmd, 执行nvim # lazygit: git's terminal ui # 在终端中git仓库目录, 执行lazygit 即可管理 choco install lazygit -y # 终端docker ui 管理工具, 类比web版portainer choco install lazydocker -y # ntop: 类似htop choco install ntop.portable -y # grep, 和pwsh自带的findstr相当 choco install grep -y # 定位位置 choco install which -y # 编程等宽字体 # 总会找到喜欢的款... choco install firacode -y choco install nerd-fonts-UbuntuMono -y choco install monaspace nerd-fonts-Monaspace -y ``` ## Scoop --- 官网:https://scoop.sh scoop同样支持命令行安装Windows软件,不过它的适用目标更有针对性: 更多是帮助开发者安装特定系统工具使用,尤其是那些依赖于Linux但在Windows系统上不存在的软件。 虽然scoop同样能够成功安装chrome等常规Windows程序,但要做到这点, 用户可能还需要额外手动再去做些其它的操作, 相较于winget与choco的一步到位略显繁琐些。 安装scoop ```powershell # 非管理员权限打开一个powershell # 执行如下命令 # 修改当前用户的执行策略 # 可选: 如果之前安装choco或者其他时配置过, 可以跳过 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # 开始安装 Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression ``` 验证安装与详细参数 ```powershell PS C:\Users\*******> Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression Initializing... Downloading... Creating shim... Adding ~\scoop\shims to your path. Scoop was installed successfully! Type 'scoop help' for instructions. # 版本信息 PS C:\Users\*******> scoop -v Current Scoop version: 859d1db5 (HEAD -> master, tag: v0.5.2, origin/master, origin/HEAD) chore(release): Bump to version 0.5.2 (#6080) 'main' bucket: ca3d19d87 (HEAD -> master, origin/master, origin/HEAD) fastfetch: Update to version 2.23.0 PS C:\Users\*******> scoop --help Usage: scoop <command> [<args>] Available commands are listed below. Type 'scoop help <command>' to get more help for a specific command. Command Summary alias Manage scoop aliases bucket Manage Scoop buckets cache Show or clear the download cache cat Show content of specified manifest. checkup Check for potential problems cleanup Cleanup apps by removing old versions config Get or set configuration values create Create a custom app manifest depends List dependencies for an app, in the order they'll be installed download Download apps in the cache folder and verify hashes export Exports installed apps, buckets (and optionally configs) in JSON format help Show help for a command hold Hold an app to disable updates home Opens the app homepage import Imports apps, buckets and configs from a Scoopfile in JSON format info Display information about an app install Install apps list List installed apps prefix Returns the path to the specified app reset Reset an app to resolve conflicts search Search available apps shim Manipulate Scoop shims status Show status and check for new app versions unhold Unhold an app to enable updates uninstall Uninstall an app update Update apps, or Scoop itself virustotal Look for app's hash or url on virustotal.com which Locate a shim/executable (similar to 'which' on Linux) ``` 在线App查找: https://scoop.sh/#/apps * 输入想要用的软件, 根据网站给出的命令安装 scoop更多用法待补充... THE END 本文作者:将夜 本文链接:http://zoe.red/2024/598.html 版权声明:本博客所有文章除特别声明外,均默认采用 CC BY-NC-SA 4.0 许可协议。 最后修改:2024 年 09 月 13 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏