PowerShell 和 Windows PowerShell 是两个独立的产品,虽然名字相似,但底层技术和定位完全不同

Windows PowerShell 是 2006 年随 Windows 推出的“老将”,基于 .NET Framework,只能跑在 Windows 上。

PowerShell 7(早期叫 PowerShell Core)是微软在 2016 年开源的“继任者”,基于跨平台的 .NET Core / .NET 5+ 重写。它不仅能跑在 Windows 上,还能跑在 Linux 和 macOS 上

一、升级到 PowerShell 7

Windows 自带的 PowerShell 5.1 基于 .NET Framework,启动速度和模块加载效率都不理想。PowerShell 7 基于 .NET Core,启动更快,且与 5.1 并行运行,安装后不会覆盖系统原有的 powershell.exe

安装方式(WinGet)

winget install --id Microsoft.PowerShell --source winget

安装完成后,可执行文件为 pwsh.exe,在 Windows Terminal 中新建标签页时选择 “PowerShell” 即可使用。官方安装文档(MSI、ZIP 等其他方式):在 Windows 上安装 PowerShell 7 - Microsoft Learn-1

二、配置 $PROFILE 文件

所有增强配置都写入 $PROFILE 文件。PowerShell 7 的用户级配置文件路径为 text $HOME\Documents\PowerShell\Microsoft.PowerShell\_profile.ps1

如果文件不存在,先创建:

if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }
notepad $PROFILE

三、增强 PSReadLine(交互体验核心)

PowerShell 7 内置了较新版本的 PSReadLine,这是命令行交互体验的基础模块。

将以下配置加入 $PROFILE:

# 开启预测性 IntelliSense(基于历史记录)
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView

# 上下箭头:只搜索匹配前缀的历史命令
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

# Tab 键:弹出菜单选择而非循环切换
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

官方 PSReadLine 模块文档:PSReadLine Module - Microsoft Learn-2。

四、posh-git(Git 状态提示)

如果你使用 Git,这个模块会在提示符中实时显示分支名和状态符号。

安装:

Install-Module posh-git -Scope CurrentUser -Force

在 $PROFILE 中添加导入:

Import-Module posh-git

配置后提示符会显示类似 [main ≡ +0 ~2 -1] 的信息,直观展示分支、领先/落后提交数和工作区状态。

五、PSFzf(模糊查找)

PSFzf 将 fzf 模糊查找器深度集成到 PowerShell 中,Ctrl+R 模糊搜索历史命令,Ctrl+T 模糊查找文件路径,效率远高于默认方式。

安装 PSFz

Install-Module -Name PSFzf -Scope CurrentUser -Force

在 $PROFILE 中配置:

Import-Module PSFzf
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'

PSFzf 官方仓库:kelleyma49/PSFzf - GitHub

六、zoxide(智能目录跳转)

zoxide 会记录你常去的目录,之后用 z 关键词 即可跳转,省去输入完整路径。

安装:

winget install ajeetdsouza.zoxide

在 $PROFILE 末尾添加初始化:


Invoke-Expression (& { (zoxide init powershell | Out-String) })

重要:如果同时使用 Oh My Posh 或 Starship,zoxide 的初始化必须放在提示符主题初始化之后,否则会被覆盖。zoxide 官方仓库:ajeetdsouza/zoxide - GitHub-5。

七、Oh My Posh(终端美化)

Oh My Posh 是 PowerShell 提示符美化的事实标准,支持 Git 状态、路径、系统信息等丰富展示。

安装:

winget install JanDeDobbeleer.OhMyPosh --source winget

配置(加入 $PROFILE):

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

字体要求:Oh My Posh 的许多主题使用 Nerd Font 图标,需要安装并配置 Nerd Font(推荐 Cascadia Code NF),否则图标会显示为方块-18。在 Windows Terminal 设置中,为 PowerShell 配置文件的 “外观 → 字体” 选择已安装的 Nerd Font。

官方安装指南:Oh My Posh 官网-6;Windows Terminal 配置教程:Custom prompt setup - Microsoft Learn-18。

八、Terminal-Icons(文件图标)

为 ls / Get-ChildItem 的输出添加文件类型图标,视觉上更易区分。

安装:

Install-Module -Name Terminal-Icons -Repository PSGallery -Scope CurrentUser -Force

在 $PROFILE 中添加:

Import-Module Terminal-Icons

注意:同样需要 Nerd Font 支持-16。官方仓库:devblackops/Terminal-Icons - GitHub-16。

九、性能优化补充

如果遇到脚本执行慢的问题,PowerShell 官方有几条核心建议-17:

  • 避免在循环内重复调用函数:函数调用成本高,应把循环移到函数内部。
  • 谨慎使用 Write-Host:Write-Host 比写入管道的输出慢得多,仅当确实需要直接输出到控制台时使用。
  • 优先使用 ForEach-Object -Parallel:PowerShell 7 的并行执行比 Start-Job 轻量得多。官方测试中,ForEach-Object -Parallel 处理 20 个任务耗时 0.79 秒,而 Start-Job 需要 7.58 秒-8。

并行执行官方文档:使用并行执行优化性能 - Microsoft Learn-8。