Windows Terminal 优化
Windows 推出的新终端 Windows Teminal 使用上要比默认的 PowerShell 体验好不少。通过 Windows Terminal 配合 PowerShell 7 使用效果更佳。
安全策略#
计算机上启动 Windows PowerShell 时,执行策略很可能是 Restricted(默认设置)。
Restricted执行策略不允许任何脚本运行。AllSigned和RemoteSigned执行策略可防止Windows PowerShell运行没有数字签名的脚本。
因此如果直接安装很可能出现无法运行脚本的错误。
查看当前策略#
打开 Windows PowerShell 然后输入 get-executionpolicy,如果出现下面的内容则需要修改安全策略。
PS C:\WINDOWS\system32> get-executionpolicyRestricted修改策略#
以管理员身份打开 Windows Posershell,执行下列命令:
set-executionpolicy remotesigned# 运行后输入 Y安装 Windows 终端#
直接在微软商店搜索下载 Windows Terminal,或者输入 终端。
安装 PowerShell 7#
按照官方的 教程 或者直接使用下面的命令:
winget install --id Microsoft.Powershell --source winget安装好后打开 Windows 终端,在 设置-启动-默认配置文件 中修改为 pwsh,注意不是 Windows PowerShell,保存修改。
如果提示没有 winget 命令,首先通过微软商店安装,参考 官方文档。
WinGet 命令行工具仅在 Windows 10 1709(版本 16299)或更高版本上受支持。 在你首次以用户身份登录 Windows(这会触发 Microsoft Store 将 Windows 程序包管理器注册为异步进程的一部分)之前,WinGet 工具不可用。
如果最近已经以用户身份进行了首次登录,但发现 WinGet 尚不可用,则可以打开 PowerShell 并输入以下命令来请求此 WinGet 注册:Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe。
安装 Scoop#
Scoop.sh 是 Windows 上的一个包管理器,采用滚动更新的方式,使用和下载软件方便,推荐使用,需自行解决网络问题。
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUserInvoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression安装 Nerd Font 字体#
接下来安装并设置字体。使用等宽字体达到更好的显示效果,推荐使用 Maple Font,官网:Maple Mono
scoop bucket add nerd-fontsscoop install nerd-fonts/Maple-Mono-NF-CN终端配置#
在 Windows Terminal 中输入下面第一行命令创建新配置文件,创建后输入两条命名创建配置文件并使用记事本打开:
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }notepad $profileStarship#
一个自定义的 Prompt 插件,相比 Oh My Posh 更加轻量,主题没有 OMP 多,但是配置简单。
scoop install starship在 $PROFILE 中添加配置:
Invoke-Expression (&starship init powershell)设置代理#
在 $PROFILE 中添加函数和别名,方便快速设置代理或者清除代理
# ==================== 代理管理 ====================
# 默认回退代理(系统代理不可用时使用)$script:DefaultProxyUrl = "http://127.0.0.1:7890"
# 获取系统代理配置function Get-SystemProxy { $result = @{ Enabled = $false; Url = $null } try { $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" $reg = Get-ItemProperty -Path $regPath -ErrorAction Stop if ($reg.ProxyEnable -eq 1 -and $reg.ProxyServer) { $server = $reg.ProxyServer
# 处理 "http=host:port;https=host:port" 格式 if ($server -match "=") { $pair = ($server -split ";") | Where-Object { $_ -match "^https?=" } | Select-Object -First 1 if ($pair) { $server = ($pair -split "=", 2)[1] } }
# 去掉可能已有的协议前缀 $server = $server -replace "^[a-zA-Z]+://", ""
if ($server -match "^(.+):(\d+)$") { $result = @{ Enabled = $true Host = $matches[1] Port = $matches[2] Url = "http://$server" } } } } catch { Write-Warning "读取系统代理失败: $($_.Exception.Message)" } return $result}
# 启用代理function Enable-Proxy { param([string]$ProxyUrl)
if (-not $ProxyUrl) { $sys = Get-SystemProxy if ($sys.Enabled) { $ProxyUrl = $sys.Url } else { $ProxyUrl = $script:DefaultProxyUrl Write-Warning "未检测到系统代理,使用默认值: $ProxyUrl" } }
$env:HTTP_PROXY = $ProxyUrl $env:HTTPS_PROXY = $ProxyUrl $env:ALL_PROXY = $ProxyUrl $env:NO_PROXY = "localhost,127.0.0.1,::1"
# 仅影响当前 PowerShell 会话中后续创建的 .NET 请求 [System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebProxy]::new($ProxyUrl, $true)
Write-Host "✓ Proxy enabled: $ProxyUrl" -ForegroundColor Green}
# 取消代理function Disable-Proxy { foreach ($name in "HTTP_PROXY","HTTPS_PROXY","ALL_PROXY","NO_PROXY") { Remove-Item "Env:$name" -ErrorAction SilentlyContinue } # 还原为系统默认代理,而不是 $null [System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
Write-Host "✓ Proxy disabled" -ForegroundColor Yellow}
# 当前会话实际使用的代理function Get-ActiveProxy { if ($env:HTTPS_PROXY) { return $env:HTTPS_PROXY } if ($env:HTTP_PROXY) { return $env:HTTP_PROXY } return $null}
# 状态 / 连通性检查:pstatus 仅看状态,pstatus -Test 同时测试连通性function Get-ProxyStatus { param([switch]$Test)
$sys = Get-SystemProxy Write-Host "==================== Proxy Status ====================" -ForegroundColor Cyan Write-Host "System proxy : $(if ($sys.Enabled) { $sys.Url } else { 'disabled' })" Write-Host "HTTP_PROXY : $(if ($env:HTTP_PROXY) { $env:HTTP_PROXY } else { '(not set)' })" Write-Host "HTTPS_PROXY : $(if ($env:HTTPS_PROXY) { $env:HTTPS_PROXY } else { '(not set)' })" Write-Host "ALL_PROXY : $(if ($env:ALL_PROXY) { $env:ALL_PROXY } else { '(not set)' })"
if ($Test) { Write-Host "`n------------------ Connectivity ------------------" -ForegroundColor Cyan $active = Get-ActiveProxy $sites = @( @{ Name = "Google"; Url = "https://www.google.com" }, @{ Name = "GitHub"; Url = "https://github.com" }, @{ Name = "GitHub Raw"; Url = "https://raw.githubusercontent.com" }, @{ Name = "PowerShell Gallery"; Url = "https://www.powershellgallery.com" } ) foreach ($site in $sites) { Write-Host ("Testing {0,-20}" -f $site.Name) -NoNewline try { $params = @{ Uri = $site.Url; Method = "Head"; TimeoutSec = 10 UseBasicParsing = $true; ErrorAction = "Stop" } if ($active) { $params.Proxy = $active } $null = Invoke-WebRequest @params Write-Host "✓ Accessible" -ForegroundColor Green } catch { # 收到 HTTP 响应(如 405)也说明网络是通的 if ($_.Exception.Response) { Write-Host "✓ Reachable (HTTP $([int]$_.Exception.Response.StatusCode))" -ForegroundColor Green } else { Write-Host "✗ Not accessible" -ForegroundColor Red } } } } Write-Host "======================================================" -ForegroundColor Cyan}
function Show-ProxyHelp { Write-Host "PowerShell Proxy Management" -ForegroundColor Cyan Write-Host " pset [-ProxyUrl url] 启用代理(默认读取系统代理,失败则用 $script:DefaultProxyUrl)" Write-Host " punset 取消代理" Write-Host " pstatus [-Test] 查看状态;加 -Test 同时测试连通性" Write-Host " phelp 显示帮助"}
New-Alias -Name pset -Value Enable-Proxy -ForceNew-Alias -Name punset -Value Disable-Proxy -ForceNew-Alias -Name pstatus -Value Get-ProxyStatus -ForceNew-Alias -Name phelp -Value Show-ProxyHelp -Force
Write-Host "Proxy management loaded. Type 'phelp' for help." -ForegroundColor Green
if ((Get-SystemProxy).Enabled) { pset }WSL2#
WSL2 的镜像模式可以直接使用宿主机 Windows 的系统代理,但是 SSH 连接不会使用,因此需要手动设置。
SSH 代理#
使用 netcat 配置代理:
apt update && apt install netcat-openbsd -y编辑 ~/.ssh/config(没有就新建):
Match host github.com exec "test -n \"$ALL_PROXY$HTTPS_PROXY$HTTP_PROXY$all_proxy$https_proxy$http_proxy\"" ProxyCommand nc -X connect -x 127.0.0.1:7890 %h %p
Host github.com HostName github.com User git Port 22常见问题#
去除更新提示#
添加环境变量 POWERSHELL_UPDATECHECK
Off关闭更新通知功能DEFAULT不定义LTS仅通知长期服务版本更新
去除更新启动速度提示#
在 setting.json 中进行如下设置:
"commandline": "powershell.exe -nologo"# 或者"commandline": "\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -nologo"