PowerShell 脚本来自动安装 jq 、配置环境变量

一个 💡适用于 Windows 的一键 PowerShell 脚本,会完成以下功能:

  1. 检查是否已安装 jq
  2. 如果没有,则自动下载 jq-win64.exe
  3. 将其保存到 C:\Tools\jq(可修改)
  4. 自动将此目录添加到系统环境变量 PATH

✅ 一键安装 jq 的 PowerShell 脚本

可以将以下内容保存为 install-jq.ps1 文件,然后右键选择"以管理员身份运行"

php 复制代码
# 设置目标目录
$jqDir = "C:\Tools\jq"
$jqExe = "$jqDir\jq.exe"
$jqUrl = "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-win64.exe"

# 如果 jq 已存在则提示
if (Test-Path $jqExe) {
    Write-Host "✅ jq 已安装在 $jqExe" -ForegroundColor Green
    exit
}

# 创建目标目录
if (-Not (Test-Path $jqDir)) {
    New-Item -ItemType Directory -Path $jqDir | Out-Null
    Write-Host "📁 创建目录: $jqDir"
}

# 下载 jq.exe
Write-Host "⬇️ 正在下载 jq..." -ForegroundColor Cyan
Invoke-WebRequest -Uri $jqUrl -OutFile $jqExe

# 设置执行权限
Unblock-File -Path $jqExe

# 添加到系统 PATH(如果尚未添加)
$envPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine)
if ($envPath -notlike "*$jqDir*") {
    Write-Host "🔧 正在将 jq 目录添加到系统环境变量 PATH..." -ForegroundColor Yellow
    [System.Environment]::SetEnvironmentVariable("Path", "$envPath;$jqDir", [System.EnvironmentVariableTarget]::Machine)
    Write-Host "✅ 已成功添加到 PATH!请重新打开终端以生效。" -ForegroundColor Green
} else {
    Write-Host "✅ jq 路径已在系统 PATH 中" -ForegroundColor Green
}

# 测试安装
Write-Host "`n✨ 你现在可以输入 'jq --version' 来验证安装结果。" -ForegroundColor Cyan

📌 使用说明:

  1. 将代码保存为 install-jq.ps1
  2. 右键以管理员身份运行(添加系统 PATH 需要管理员权限);
  3. 成功后你就可以在任何终端中使用:
css 复制代码
jq --version

如果使用的是 普通 PowerShell ,默认可能禁用了 .ps1 脚本执行,可以先运行这条命令来允许当前用户运行脚本:

sql 复制代码
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

相关推荐
Irissgwe13 小时前
四、进程控制(进程等待与进程程序替换,shell)
linux·shell·进程·进程等待·进程程序替换
lifewange4 天前
VMware Workstation / VirtualBox / Hyper-V对比
shell
量子炒饭大师4 天前
【Linux系统编程】Cyberpunk在霓虹丛林中构建堡垒 ——【关于shell命令及其运行原理】
linux·运维·服务器·shell
phenhorlin11 天前
我做了个工具,让切换 Homebrew 镜像像切 npm 源一样简单
后端·shell
qq_3750133325 天前
shell
shell
Strugglingler25 天前
基于whiptail开发shell导航工具
linux·shell·ui设计·whiptail
亚林瓜子1 个月前
AWS Glue Python Shell中获取外网ip
python·tcp/ip·http·shell·aws·vpc·glue
爱凤的小光1 个月前
GDB调试技巧与指令完全指南---个人学习篇
shell·gdb
亚林瓜子1 个月前
AWS Glue Python Shell任务中pip安装依赖库
python·shell·pip·aws·glue·job