Windows Terminal 优化
powershell 5 升级到 7 (默认自动补全、智能提示)
通过 starship 美化 UI
升级 powershell 到 7
查看当前版本
Get-Host | Select-Object Version即便是最新版的 Win11 24H2,powershell 的默认版本依然是 5
Version
-------
5.1.26100.4061这里建议用 winget 升级
winget search Microsoft.PowerShell
winget install --id Microsoft.PowerShell --source winget此时 WT 已识别 powershell 7

WT 区分 powershell 5、7 版本
打开 WT 的 settings.json:设置 > 左下角的“打开 JSON 文件”,修改 profiles.list
name 由 Windows PowerShell 改为 PowerShell 5
{
"commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"name": "PowerShell 5"
}name 由 PowerShell 改为 PowerShell 7
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
"hidden": false,
"name": "PowerShell 7",
"source": "Windows.Terminal.PowershellCore"
}WT 默认启动 powshell 7
defaultProfile 改为 powshell 7 的 guid
{
"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}"
}psh 5 配置文件迁移到 7
查看配置文件路径,分别在 psh5、7 下执行
echo $PROFILEpsh5:%USERPROFILE%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
psh7:%USERPROFILE%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
由此可见区别在于目录名,5 的目录名为 WindowsPowerShell,7 的目录名为 PowerShell
默认是没有 7 的路径,需要手动创建。然后将 5 的 Microsoft.PowerShell_profile.ps1 内容复制到 7 即可完成迁移
starship 安装、配置
安装 starship
scoop install starship安装并在终端启用 Nerd Font 字体 (前置要求),以 JetBrains Mono 为例
scoop install JetBrainsMono-NF
将以下内容添加到您 PowerShell 配置文件的末尾(通过运行 $PROFILE 来获取配置文件的路径)
Invoke-Expression (&starship init powershell)PowerShell 配置文件位于 %USERPROFILE%/Documents/PowerShell/Microsoft.PowerShell_profile.ps1
此时 starship 的默认主题已生效,如下

使用 starship 社区配置 (主题)
创建 starship 配置文件,默认路径如下
%USERPROFILE%/.config/starship.toml这里选择 Catppuccin Powerline 主题
将 TOML 内容复制到上述配置文件中:https://starship.rs/presets/toml/catppuccin-powerline.toml
此时 starship 的 Catppuccin Powerline 主题已生效,如下

更多预设主题:https://starship.rs/zh-CN/presets/
修改 starship 配置文件默认路径 (可选)
两种方法:
1 设置系统环境变量 STARSHIP_CONFIG,设置为当前用户的环境变量(不需要管理员权限)
[Environment]::SetEnvironmentVariable("STARSHIP_CONFIG", "$HOME\path\starship.toml", "User")2 通过 powershell 配置文件
# 打开配置文件
notepad $PROFILE
# 在文件中添加这一行
$ENV:STARSHIP_CONFIG = "path\starship.toml"