Windows Terminal 优化

Windows 终端配置记录。

#windows#terminal

Windows Terminal 优化

Windows 推出的新终端 Windows Teminal 使用上要比默认的 PowerShell 体验好不少。通过 Windows Terminal 配合 PowerShell 7 使用效果更佳。

安全策略#

计算机上启动 Windows PowerShell 时,执行策略很可能是 Restricted(默认设置)。

因此如果直接安装很可能出现无法运行脚本的错误。

查看当前策略#

打开 Windows PowerShell 然后输入 get-executionpolicy,如果出现下面的内容则需要修改安全策略。

Terminal window
PS C:\WINDOWS\system32> get-executionpolicy
Restricted

修改策略#

以管理员身份打开 Windows Posershell,执行下列命令:

Terminal window
set-executionpolicy remotesigned
# 运行后输入 Y

安装 Windows 终端#

直接在微软商店搜索下载 Windows Terminal,或者输入 终端

安装 PowerShell 7#

按照官方的 教程 或者直接使用下面的命令:

Terminal window
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 上的一个包管理器,采用滚动更新的方式,使用和下载软件方便,推荐使用,需自行解决网络问题。

Terminal window
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression

安装 Nerd Font 字体#

接下来安装并设置字体。使用等宽字体达到更好的显示效果,推荐使用 Maple Font,官网:Maple Mono

Terminal window
scoop bucket add nerd-fonts
scoop install nerd-fonts/Maple-Mono-NF-CN

终端配置#

Windows Terminal 中输入下面第一行命令创建新配置文件,创建后输入两条命名创建配置文件并使用记事本打开:

Terminal window
if (!(Test-Path -Path $PROFILE )) { New-Item -Type File -Path $PROFILE -Force }
notepad $profile

Starship#

一个自定义的 Prompt 插件,相比 Oh My Posh 更加轻量,主题没有 OMP 多,但是配置简单。

Terminal window
scoop install starship

$PROFILE 中添加配置:

Terminal window
Invoke-Expression (&starship init powershell)

设置代理#

$PROFILE 中添加函数和别名,方便快速设置代理或者清除代理

Terminal window
# 代理管理
# 自动获取系统代理配置,支持代理设置、取消、状态检查和连通性测试
# 获取系统代理配置
function Get-SystemProxy {
try {
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
$proxyEnable = (Get-ItemProperty -Path $regPath -Name ProxyEnable -ErrorAction SilentlyContinue).ProxyEnable
$proxyServer = (Get-ItemProperty -Path $regPath -Name ProxyServer -ErrorAction SilentlyContinue).ProxyServer
if ($proxyEnable -eq 1 -and $proxyServer) {
# 解析代理服务器地址
if ($proxyServer -match "^(.+):(\d+)$") {
return @{
Enabled = $true
Host = $matches[1]
Port = $matches[2]
Url = "http://$proxyServer"
}
}
}
}
catch {
Write-Warning "Failed to get system proxy settings: $($_.Exception.Message)"
}
# 如果无法获取系统代理,使用默认值
return @{
Enabled = $false
Host = "127.0.0.1"
Port = "7890"
Url = "http://127.0.0.1:7890"
}
}
# 设置代理
function Enable-Proxy {
param(
[string]$ProxyUrl = $null
)
if (-not $ProxyUrl) {
$systemProxy = Get-SystemProxy
$ProxyUrl = $systemProxy.Url
}
try {
# 设置环境变量
$env:HTTP_PROXY = $ProxyUrl
$env:HTTPS_PROXY = $ProxyUrl
$env:ALL_PROXY = $ProxyUrl
# 设置 .NET 应用程序的代理
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy($ProxyUrl, $true)
[System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy($ProxyUrl, $true)
Write-Host "✓ Proxy enabled: $ProxyUrl" -ForegroundColor Green
}
catch {
Write-Error "Failed to enable proxy: $($_.Exception.Message)"
}
}
# 取消代理
function Disable-Proxy {
try {
# 清除环境变量
$env:HTTP_PROXY = $null
$env:HTTPS_PROXY = $null
$env:ALL_PROXY = $null
# 清除 .NET 应用程序的代理
[System.Net.WebRequest]::DefaultWebProxy = $null
[System.Net.Http.HttpClient]::DefaultProxy = $null
Write-Host "✓ Proxy disabled" -ForegroundColor Yellow
}
catch {
Write-Error "Failed to disable proxy: $($_.Exception.Message)"
}
}
# 检查代理状态
function Get-ProxyStatus {
$systemProxy = Get-SystemProxy
Write-Host "==================== Proxy Status ====================" -ForegroundColor Cyan
Write-Host "System Proxy Configuration:" -ForegroundColor White
Write-Host " Enabled: $($systemProxy.Enabled)" -ForegroundColor Gray
Write-Host " Host: $($systemProxy.Host)" -ForegroundColor Gray
Write-Host " Port: $($systemProxy.Port)" -ForegroundColor Gray
Write-Host " URL: $($systemProxy.Url)" -ForegroundColor Gray
Write-Host ""
Write-Host "Current Environment Settings:" -ForegroundColor White
if ($env:HTTP_PROXY -or $env:HTTPS_PROXY -or $env:ALL_PROXY) {
Write-Host " HTTP_PROXY: $($env:HTTP_PROXY)" -ForegroundColor Gray
Write-Host " HTTPS_PROXY: $($env:HTTPS_PROXY)" -ForegroundColor Gray
Write-Host " ALL_PROXY: $($env:ALL_PROXY)" -ForegroundColor Gray
} else {
Write-Host " No proxy environment variables set" -ForegroundColor Gray
}
Write-Host "=======================================================" -ForegroundColor Cyan
}
# 测试连通性
function Test-ProxyConnectivity {
Write-Host "==================== Connectivity Test ====================" -ForegroundColor Cyan
$sites = @(
@{ Name = "Google"; Url = "https://www.google.com" },
@{ Name = "GitHub"; Url = "https://github.com" },
@{ Name = "GitHub Raw"; Url = "https://raw.githubusercontent.com" }
)
foreach ($site in $sites) {
Write-Host "Testing $($site.Name)... " -NoNewline
try {
$response = Invoke-WebRequest -Uri $site.Url -Method Head -TimeoutSec 10 -UseBasicParsing -ErrorAction Stop
if ($response.StatusCode -eq 200) {
Write-Host "✓ Accessible" -ForegroundColor Green
} else {
Write-Host "✗ Not accessible (Status: $($response.StatusCode))" -ForegroundColor Red
}
}
catch {
Write-Host "✗ Not accessible" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "Testing PowerShell Gallery... " -NoNewline
try {
$null = Find-Module -Name PowerShellGet -ErrorAction Stop
Write-Host "✓ Accessible" -ForegroundColor Green
}
catch {
Write-Host "✗ Not accessible" -ForegroundColor Red
}
Write-Host "============================================================" -ForegroundColor Cyan
}
# 综合检查(状态 + 连通性)
function Test-ProxyAll {
Get-ProxyStatus
Write-Host ""
Test-ProxyConnectivity
}
# 显示帮助
function Show-ProxyHelp {
Write-Host "PowerShell Proxy Management Functions" -ForegroundColor Cyan
Write-Host ""
Write-Host "Available Commands:" -ForegroundColor White
Write-Host " pset Enable proxy (auto-detect or manual)"
Write-Host " punset Disable proxy"
Write-Host " pstatus Check proxy status"
Write-Host " ptest Test network connectivity"
Write-Host " pcheck Check proxy status + test connectivity"
Write-Host " phelp Show this help message"
Write-Host ""
Write-Host "Examples:" -ForegroundColor White
Write-Host " pset # Enable proxy (auto-detect)"
Write-Host " pset -ProxyUrl 'http://proxy:8080' # Enable with custom proxy"
Write-Host " punset # Disable proxy"
Write-Host " pcheck # Check status and test connectivity"
Write-Host ""
Write-Host "System Integration:" -ForegroundColor White
Write-Host " The script automatically detects system proxy settings from Windows registry"
Write-Host " If system proxy is not available, it falls back to 127.0.0.1:7890"
}
# 别名
New-Alias -Name pset -Value Enable-Proxy -Force
New-Alias -Name punset -Value Disable-Proxy -Force
New-Alias -Name pstatus -Value Get-ProxyStatus -Force
New-Alias -Name ptest -Value Test-ProxyConnectivity -Force
New-Alias -Name pcheck -Value Test-ProxyAll -Force
New-Alias -Name phelp -Value Show-ProxyHelp -Force
# 启动消息
Write-Host "Proxy management functions loaded. Type 'phelp' for help." -ForegroundColor Green

常见问题#

去除更新提示#

添加环境变量 POWERSHELL_UPDATECHECK

去除更新启动速度提示#

setting.json 中进行如下设置:

Terminal window
"commandline": "powershell.exe -nologo"
# 或者
"commandline": "\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -nologo"

启动速度慢#

安装 Anaconda 或者 miniconda 后,终端每次启动会初始化 conda 确保可以直接使用 conda,这会拖慢启动速度,可以禁用或者延迟加载。

安装后会有一个 profile.ps1 文件,内容如下:

Terminal window
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "C:\Users\tom\miniconda3\Scripts\conda.exe") {
(& "C:\Users\tom\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion

可以直接删除,或者延迟加载:

Terminal window
function miniconda {
(& "C:\Users\tom\miniconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}

Note

  • 函数名称可以自行更改,在使用前输入函数名即可初始化
  • conda 可执行文件路径替换为自己的路径

参考#

版权许可

CC BY-NC-SA 4.0 许可协议。

相关文章