PowerToys 如何启用 Command Not Found 模块并在终端获得 winget 安装建议【免费下载链接】PowerToysMicrosoft PowerToys is a collection of utilities that supercharge productivity and customization on Windows项目地址: https://gitcode.com/GitHub_Trending/po/PowerToys当你在 PowerShell 终端里敲入一个没有安装的命令例如kubectl时通常只会看到 not recognized 报错。PowerToys 的 Command Not Found 模块会拦截这类命令未找到的错误查询已知软件包仓库并在终端中给出对应的 winget 安装建议。本文的目标是在已安装 PowerToys 的 Windows 系统上启用该模块验证它确实生效并知道如何还原。工作原理与依赖要求根据 Command Not Found 开发文档该模块的核心是 Microsoft.WinGet.CommandNotFound 这个 PowerShell 模块它注册一个 command-not-found 处理程序拦截命令执行失败并基于 WinGet 仓库中可用的包给出安装建议。启用之前需要满足三个依赖条件这些条件由检查脚本 CheckCmdNotFoundRequirements.ps1 逐项判断PowerShell 7.4 或更高版本脚本用$PSVersionTable.PSVersion -ge 7.4判断不满足时输出 PowerShell 7.4 or greater not detected.Microsoft.WinGet.Client 模块版本不低于1.8.1133版本过低时输出 WinGet Client module needs to be updated.未安装时输出 WinGet Client module not detected.Microsoft.WinGet.CommandNotFound 模块启用模块时会自动安装无需手动准备。在 PowerToys 设置中启用模块在 PowerToys 设置里打开 Command Not Found 页面并启用模块。设置页面背后会做两件事对应的脚本都在 Settings\Scripts 目录下第一步检查环境。设置页加载时会以如下方式调用检查脚本调用方式取自 CmdNotFoundViewModel.cs 中的实际参数pwsh.exe -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -File PowerToys 设置 UI 安装目录\Assets\Settings\Scripts\CheckCmdNotFoundRequirements.ps1其中PowerToys 设置 UI 安装目录需要替换为你机器上 PowerToys 设置界面程序所在的目录即包含Assets\Settings\Scripts子目录的那一层。脚本会输出上面三条依赖检查的结论设置页面据此显示各项要求是否满足。第二步安装并注册模块。点击启用后执行 EnableModule.ps1它有以下几个明确的副作用执行前需要知道如果Microsoft.WinGet.CommandNotFound模块未安装会执行Install-Module -Name Microsoft.WinGet.CommandNotFound -Force从模块库下载并安装该模块如果$PROFILE文件不存在会创建它随后在$PROFILE中追加或升级注册内容脚本开头还会启用 PowerShell 实验性功能PSFeedbackProvider和PSCommandNotFoundSuggestion仅当系统中列出了这些实验性功能时。写入 Profile 的注册内容是这三行带有一对标记注释#f45873b3-b655-43a6-b217-97c00aa0db58 PowerToys CommandNotFound module Import-Module -Name Microsoft.WinGet.CommandNotFound #f45873b3-b655-43a6-b217-97c00aa0db58如果之前注册过旧版本标记为34de4b3d-13a8-4540-b76d-b9e8d3851756的Import-Module旧路径写法脚本会自动把旧写法替换为上面的按名称导入写法并把标记更新为新的 GUID。成功判定脚本会输出以下三种结果之一设置页面以这三种输出作为安装成功条件Module was successfully registered in the profile file.新注册Module was successfully upgraded in the profile file.从旧版本升级Module is already registered in the profile file.已经注册过在终端验证安装建议注册写入的是 PowerShell Profile因此要在新打开的 PowerShell 7 终端中验证已打开的会话不会加载新 Profile。输入一个未安装的命令模块就会给出安装建议。开发文档给出的示例输出如下示例结果实际建议内容取决于你输入的命令及其在软件包仓库中的匹配情况C:\ kubectl kubectl is not recognized as an internal or external command, operable program, or batch file. Command kubectl not found, but can be installed with: winget install -e --id Kubernetes.kubectl看到can be installed with: winget install ...形式的建议说明模块已正常工作。如果需要手动复查状态可以运行前面提到的检查脚本确认输出中同时包含 PowerShell 7.4 or greater detected.、WinGet Client module detected. 和 Command Not Found module is registered in the profile file. 三条信息也可以直接打开$PROFILE文件确认其中存在上面三行注册内容。还原与边界说明关闭模块在设置页卸载时会执行 DisableModule.ps1它按两行标记注释定位并删除 Profile 中的注册行。成功输出为Removed the Command Not Found reference from the profile file.如果 Profile 中没有注册记录输出No instance of Command Not Found was found in the profile file.企业环境设置页会读取 GPO 规则来判定该模块是否被组策略强制启用或禁用见 CmdNotFoundViewModel.cs 中GPOWrapper.GetConfiguredCmdNotFoundEnabledValue()的调用域内机器上如果设置开关不可用应先确认组策略配置。该模块依赖 WinGet 软件包仓库的匹配结果文档示例中的kubectl只是示例模块对未收录的命令不会给出建议。【免费下载链接】PowerToysMicrosoft PowerToys is a collection of utilities that supercharge productivity and customization on Windows项目地址: https://gitcode.com/GitHub_Trending/po/PowerToys创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考