【Power Shell】启动时自动配置http代理

发布时间 2023-12-15 21:50:57作者: 詩

背景

有时候我们经常需要在Windows Terminal,powershell内使用http代理来拉去GitHub代码、软件包等等,每次都需要手动配置很麻烦。其实我们可以使用.ps1脚本来启动。

https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-7.4

教程

文本编辑proxy.ps1文件写入一下内容:

# 设置代理地址和端口
$env:HTTP_PROXY = "http://127.0.0.1:10809"
$env:HTTPS_PROXY = "http://127.0.0.1:10809"

# 显示设置的环境变量
Write-Host "HTTP_PROXY 设置为: $($env:HTTP_PROXY)"
Write-Host "HTTPS_PROXY 设置为: $($env:HTTPS_PROXY)"


#powershell #powershell启动
wt     #Windows terminal启动

原理就是利用脚本在启动powershell前配置环境变量。启动的程序会继承环境变量。
image

image