devAlice
← Windows

PowerShell 7 + oh-my-posh + 模块 — 每周在 Windows Terminal 里省一小时

在 Windows Terminal 中打造日常驱动级 PowerShell 7 环境——pwsh 安装、profile 配置、oh-my-posh 主题、六个必备模块。

Windows 内置的 PowerShell 5.1 不跨平台,且存在模块兼容性问题。PowerShell 7(pwsh) 才是标准。搭配 oh-my-posh 和六个模块,可以媲美(甚至超越)macOS 的 zsh + starship 组合。

我认为 PowerShell 配置的价值不在于视觉美化,而在于从 profile 文件建立可复现的 shell 环境。以前 PowerShell 配置散落在各台机器上;如今通过 $PROFILE 和模块化配置,Windows 的 shell 体验也能达到 Mac 的水准,由于模块组合的选择,日常效率提升远超预期。

本指南是在 Windows 初始设置 安装好 winget 之后,30 分钟内完成 PowerShell 配置的步骤。

TL;DR

  1. 安装 pwsh 7winget install Microsoft.PowerShell
  2. 将 pwsh 设为默认 Windows Terminal shell
  3. 激活 Profile — 编辑 $PROFILE
  4. oh-my-posh — 提示符(Git 状态 / 执行时间 / 退出码)
  5. 六个模块 — Terminal-Icons / posh-git / PSReadLine / z / PSFzf / CompletionPredictor

前置条件

  • 已安装 winget 的 Windows 10/11(Windows 初始设置
  • 已安装 Windows Terminal(winget 或微软应用商店)
  • Nerd Font(oh-my-posh 图标所需)

1. 安装 PowerShell 7

winget install --id Microsoft.PowerShell -e

打开新标签页并运行 pwsh,确认:

$PSVersionTable.PSVersion
# Major: 7, Minor: 4+

2. 将 pwsh 设为默认 Shell

Windows Terminal → Settings(Ctrl+,)→ Startup

  • Default profile:选 PowerShell(7)——深蓝色图标(不是 5.1)

3. 配置 Profile

# Profile 文件路径
$PROFILE
# C:\Users\me\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
 
# 如不存在则创建
if (-not (Test-Path $PROFILE)) { New-Item -Type File -Path $PROFILE -Force }
 
# 编辑
notepad $PROFILE
# 或
code $PROFILE

4. 安装 oh-my-posh

winget install JanDeDobbeleer.OhMyPosh -s winget

新建终端 → 预览主题:

Get-PoshThemes

如果箭头/∋/执行时间显示为方块,说明缺少 Nerd Font

  • JetBrainsMono Nerd Font
    oh-my-posh font install JetBrainsMono
  • Windows Terminal → Settings → Profile → Appearance → Font faceJetBrainsMono Nerd Font

4.1 添加到 Profile

$PROFILE 末尾追加:

# 激活 oh-my-posh
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\jandedobbeleer.omp.json" | Invoke-Expression

主题因人而异(jandedobbeleerparadoxnegligiblecraver 等)。所有主题均位于 $env:POSH_THEMES_PATH

5. 六个模块

# 一次性全部安装
Install-Module -Name Terminal-Icons, posh-git, PSReadLine, z, PSFzf -Scope CurrentUser -Force
Install-Module -Name CompletionPredictor -AllowPrerelease -Scope CurrentUser -Force

5.1 在 Profile 中注册

$PROFILE 中:

# 5.1 图标(ls 输出中显示文件类型图标)
Import-Module Terminal-Icons
 
# 5.2 posh-git(提示符中显示 Git 状态)
Import-Module posh-git
# 如果 oh-my-posh 主题已含 git 状态,可能重复显示——二选一
 
# 5.3 PSReadLine(bash 风格按键、历史记录、补全颜色)
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineKeyHandler -Chord Tab -Function Complete  # bash 风格
 
# 5.4 z(跳转到常用目录——'z proj')
Import-Module z
 
# 5.5 PSFzf(Ctrl+R 历史记录、Ctrl+T 文件搜索)
Import-Module PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
# fzf 本体:winget install junegunn.fzf
 
# 5.6 CompletionPredictor(基于命令的 Tab 预测)
Import-Module CompletionPredictor

6. 别名 / 函数

$PROFILE 末尾添加你自己的:

# ls 替代(UNIX 友好)
Set-Alias ll Get-ChildItem
function la { Get-ChildItem -Force @args }
 
# 快速重载
function reload-profile { . $PROFILE }
 
# 即时管理员 shell
function admin { Start-Process pwsh -Verb RunAs }
 
# Git 快捷键
function gs { git status -sb }
function gd { git diff $args }
function gpl { git pull --rebase --autostash }
 
# 快捷进入 WSL
function w { wsl @args }

7. 验证

# 重载 profile
. $PROFILE
 
# 1. oh-my-posh 提示符——cd 到 Git 仓库查看分支
cd C:\Users\me\Documents\my-repo
 
# 2. PSReadLine——上方向键历史记录,输入后看到灰色建议 → 按 → 接受
git st<→ 键>   # 是否预测出「status」?
 
# 3. z——跳转到之前访问过的文件夹
z my-repo
 
# 4. Ctrl+R——交互式历史记录搜索(fzf)
 
# 5. ll——带 Terminal-Icons 的彩色 ls

故障排查

oh-my-posh 显示方块而非图标

Nerd Font 未应用。Windows Terminal → Settings → Profile → Appearance → 设置 Nerd Font。

Install-Module 权限错误

  • 首次使用:Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
  • 省略 -Scope CurrentUser 有时会提示需要管理员权限

Profile 未加载

  • pwsh 与 Windows Terminal 启动的 PowerShell 5.1(powershell.exe)的 profile 路径不同
  • 5.1:~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1
  • 7:~/Documents/PowerShell/Microsoft.PowerShell_profile.ps1
  • $PSVersionTable.PSVersion 确认版本

posh-git 很慢(大型仓库)

在大型 monorepo 中,每次提示符都执行 git status 会很慢。如果 oh-my-posh 主题已有 git 段,请禁用 posh-git,或设置 $GitPromptSettings.EnableFileStatus = $false

Ctrl+R 不工作

  • fzf 未安装——winget install junegunn.fzf
  • PSFzf 已安装但 fzf.exe 不在 PATH 中

只有灰度色彩

Windows Terminal 颜色方案设置有误。Settings → 将方案切换为 CampbellOne Half Dark

参考资料

更新日志

  • 2026-05-12:初稿。pwsh 7 + oh-my-posh + 六个模块 + 别名/函数 + 五个故障排查案例。