如何给 Windows PowerShell 配置代理

咸鱼 发布于 2026-01-18 54 次阅读


AI 摘要

这篇文章主要介绍了如何在 Windows PowerShell 中配置代理,帮助用户在需要通过代理服务器访问网络时正确设置环境变量与命令。

首先打开 PowerShell【1】, 然后打开 PowerShell 的配置文件:

notepad $profile

如若提示找不到对应文件则先执行下面这个命令创建一个 PowerShell 配置文件:
注意替换里面的 http://example.com 为你的HTTP代理链接

if (!(Test-Path -Path $profile【2】 )) { New-Item -Type File -Path $PROFILE -Force }

然后在打开的文件中添加下列内容:

Microsoft.PowerShell_profile.ps1

$regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'

function Clear-Proxy
{
Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0
Set-ItemProperty -Path $regPath -Name ProxyServer -Value ''
Set-ItemProperty -Path $regPath -Name ProxyOverride -Value ''

[Environment]::SetEnvironmentVariable('http_proxy', $null)
[Environment]::SetEnvironmentVariable('https_proxy', $null)
}

function Set-Proxy
{
$proxy = 'http://example.com'

Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 1
Set-ItemProperty -Path $regPath -Name ProxyServer -Value $proxy
Set-ItemProperty -Path $regPath -Name ProxyOverride -Value '<local>'

[Environment]::SetEnvironmentVariable('http_proxy', $proxy)
[Environment]::SetEnvironmentVariable('https_proxy', $proxy)
}
Set-Proxy

最后重启 Windows PowerShell 就可以

如若提示 在此系统上禁止运行脚本 则运行下面这个命令允许运行脚本:

set-executionpolicy remotesigned -scope currentuser
此作者没有提供个人介绍。
最后更新于 2026-01-18