Skip to content

终端 ​

PowerShell 7 ​

Windows 内置的 Windows PowerShell 5.1 与 PowerShell 7 可并行运行。后续命令均以 PowerShell 7(pwsh)为准。

安装 ​

通过 WinGet 安装:

powershell
winget install --id Microsoft.PowerShell --source winget

安装完成后,从开始菜单打开 PowerShell 7,或在终端中执行 pwsh。确认版本:

powershell
$PSVersionTable.PSVersion

WinGet 不可用时,从官方安装说明下载并安装。

更新脚本执行策略 ​

  • RemoteSigned:允许本地脚本无签名运行,但要求从互联网下载的脚本必须有数字签名。
  • Unrestricted:允许所有脚本运行,适合测试环境,但安全性较低。
powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

查看当前策略:

powershell
Get-ExecutionPolicy -List

PowerShell 配置文件 ​

  • 配置文件路径:$PROFILE
    • 通常为 ~\\Documents\\PowerShell\\Microsoft.PowerShell_profile.ps1;如果“文档”目录被重定向,实际位置可能在其他磁盘。始终以 $PROFILE 的输出为准。
  • 如果不存在,手动创建:if (-not (Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }
  • 重新加载配置文件:. $PROFILE

配置代理 ​

powershell
# ====== Proxy Start ======
function proxy_on {
    $env:http_url = "http://127.0.0.1:7897"
    $env:socks5_url = "socks5://127.0.0.1:7897"

    $env:http_proxy  = $env:http_url
    $env:https_proxy = $env:http_url
    $env:HTTP_PROXY  = $env:http_url
    $env:HTTPS_PROXY = $env:http_url
    $env:ALL_PROXY   = $env:socks5_url

    $env:no_proxy_urls = "localhost,127.0.0.1,::1,0.0.0.0,.local,host.docker.internal,gateway.docker.internal,docker.for.mac.localhost"

    $env:no_proxy = $env:no_proxy_urls
    $env:NO_PROXY = $env:no_proxy_urls

    Write-Host "终端代理已开启"
}

function proxy_off {
    Remove-Item Env:http_url   -ErrorAction SilentlyContinue
    Remove-Item Env:socks5_url -ErrorAction SilentlyContinue

    Remove-Item Env:http_proxy  -ErrorAction SilentlyContinue
    Remove-Item Env:https_proxy -ErrorAction SilentlyContinue
    Remove-Item Env:HTTP_PROXY  -ErrorAction SilentlyContinue
    Remove-Item Env:HTTPS_PROXY -ErrorAction SilentlyContinue
    Remove-Item Env:ALL_PROXY   -ErrorAction SilentlyContinue

    Remove-Item Env:no_proxy_urls -ErrorAction SilentlyContinue
    Remove-Item Env:no_proxy      -ErrorAction SilentlyContinue
    Remove-Item Env:NO_PROXY      -ErrorAction SilentlyContinue

    Write-Host "终端代理已关闭"
}

# 每次启动 PowerShell 时自动开启代理
proxy_on
# ====== Proxy End ======

基于 MIT 许可发布