效果图

为什么要美化 PowerShell?

对于许多 Windows 用户,尤其是开发者而言,Windows 默认的命令提示符和 PowerShell 界面显得过于简陋,难以满足日常开发需求。幸运的是,通过一些工具和配置,我们可以让 PowerShell 不仅美观,还兼具实用性。

接下来,我们将逐步介绍如何优化 PowerShell 界面并提升其功能。

安装与配置指南

使用 Windows Terminal 配置

Windows Terminal 是微软推出的一款现代化终端工具,它支持多标签、个性化配色方案、字体等。

1. 安装 oh-my-posh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 使用 scoop 安装 oh-my-posh
scoop install oh-my-posh

# 安装推荐字体
oh-my-posh font install meslo
scoop install Meslo-NF

# 配置字体
{
"profiles": {
"defaults": {
"font": {
"face": "MesloLGM Nerd Font"
}
}
}
}

2. 配置 PowerShell 用户文件

1
2
3
4
5
6
7
8
9
# 创建配置文件
New-Item -Path $PROFILE -Type File -Force

# 打开配置文件编辑
notepad $PROFILE

# 设置 UTF-8 编码和初始化 oh-my-posh
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
oh-my-posh init pwsh | Invoke-Expression

3. 高级自定义配置

1
2
3
4
5
# 通过环境变量指定主题
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH/jandedobbeleer.omp.json" | Invoke-Expression

# 或使用绝对路径
oh-my-posh init pwsh --config C:\Users\sfuli\scoop\apps\oh-my-posh\21.26.3\themes\jandedobbeleer.omp.json | Invoke-Expression

安装辅助模块

1
2
3
4
5
6
7
8
9
10
# 安装必需模块
scoop install psreadline posh-git

# 编辑 PowerShell 用户配置文件
notepad $PROFILE

# 添加以下配置
Set-Alias ll Get-ChildItem
Import-Module posh-git
Import-Module PSReadLine

字体与配色

字体推荐

选择合适的字体是提升终端体验的重要环节。推荐以下字体:

  • 更纱黑体
    • 中文部分使用思源黑体,英文部分使用 Iosevka,内置 Powerline 字符。
    • 推荐下载 scoop install sarasa-term-sc-nerd-font,专为终端设计。
    • 安装完成后,在 PowerShell 设置中选择该字体。

配色方案

微软推出了 ColorTool,支持 iTerm2 配色方案文件。推荐使用 Argonaut 配色方案。

1. 安装 ColorTool

1
2
# 安装 ColorTool
scoop install ColorTool

2. 配置 Argonaut 配色

Argonaut.itermcolors 文件放入 schemes 文件夹中:

1
ColorTool.exe -b Argonaut.itermcolors

注意事项

脚本执行权限

确保在“设置 > 更新和安全 > 开发者选项”中启用脚本执行。

字体兼容性问题

如果发现 oh-my-zsh 默认主题箭头符号(如 )无法显示,可通过注册表解决:

  • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontLink\SystemLink 中添加多字符串值。
  • 名称为 Sarasa Term SC,内容为:
1
2
3
4
5
6
MICROSS.TTF,Microsoft Sans Serif
MSMINCHO.TTC,MS PMincho
BATANG.TTC,Batang
MSYH.TTC,Microsoft YaHei UI
MALGUN.TTF,Malgun Gothic
SEGUISYM.TTF,Segoe UI Symbol

配置保存

完成设置后,请记得点击“确定”保存。

总结

经过上述步骤,你的 PowerShell 已焕然一新,拥有现代化的界面与增强的功能。无论是开发还是日常操作,都能获得更加高效、愉悦的体验。