Windows 10⁄11 自带的是 PowerShell 5.1(可执行文件名 powershell.exe),而微软目前主力维护的是 PowerShell 7+(可执行文件名 pwsh.exe)。
两者的 Profile 文件路径不同:
PowerShell 5.1 配置文件路径: C:\Users\<用户名>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PowerShell 7+ 配置文件路径: C:\Users\<用户名>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
安装了高版本的powershell后,建议在 Windows Terminal 中把默认 Profile 切换为 PowerShell 7。
可以使用winget install Microsoft.PowerShell
也可以去官网下载powershell ,目前官网推荐的版本是 7.xInstall-Module PSReadLine -Force -SkipPublisherCheck
Install-Module Terminal-Icons -Force
Install-Module posh-git -Force推荐使用winget安装,命令如下:
winget install JanDeDobbeleer.OhMyPosh --source wingetOh My Posh 大量使用图标字符,需要安装 Nerd Font 才能正确显示。
安装完成后,在终端设置中将字体改为对应的 Nerd Font(如 FiraCode Nerd Font、Hack Nerd Font 等)。
如果使用的是 Windows Terminal,打开 设置 → 配置文件 → 外观 → 字体,选择已安装的 Nerd Font。
安装字体:
oh-my-posh font install
在终端设置中将字体改为对应的 Nerd Font(如 FiraCode Nerd Font)
重启终端(不是新开标签页,是完全关闭再打开)https://ohmyposh.dev/docs/themes/
选择一个喜欢的主题,将它下载到本地。例如我这里下载的是 aliens.omp.json ,放到了 C:\powershell_settings\aliens.omp.json 这个自己创建的目录下。winget install zoxide编辑配置文件 notepad $PROFILE 填入如下的内容
# 我这里用了claudecode,为了方便使用还自定义一个名为 'cc' 的函数别名
function cc {
claude --dangerously-skip-permissions @args
}
# ---------- PSReadLine ----------
$PSReadLineOptions = @{
EditMode = "Windows"
PredictionSource = "History"
PredictionViewStyle = "ListView"
Colors = @{
Command = "Yellow"
Parameter = "Green"
String = "DarkCyan"
}
}
Set-PSReadLineOption @PSReadLineOptions
# Tab 补全显示为菜单(而非逐个切换)
Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete
# 上下箭头按前缀搜索历史命令
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
# Ctrl+d 删除光标处字符(类 Bash),Ctrl+l 清屏
Set-PSReadLineKeyHandler -Key Ctrl+d -Function DeleteChar
Set-PSReadLineKeyHandler -Key Ctrl+l -Function ClearScreen
# -------------------- Oh My Posh --------------------
oh-my-posh init pwsh --config "C:\powershell_settings\aliens.omp.json" | Invoke-Expression
# -------------------- 模块 --------------------
Import-Module Terminal-Icons
Import-Module posh-git
# ---------- zoxide ----------
Invoke-Expression (& { (zoxide init powershell | Out-String) })
# -------------------- 别名 --------------------
Set-Alias ll Get-ChildItem
Set-Alias which Get-Command
# -------------------- Git 快捷命令 --------------------
function gs { git status }
function gl { git log --oneline -15 }
function gd { git diff }
function gp { git push }
function gpl { git pull }
# -------------------- 自定义函数 --------------------
function Edit-Profile { notepad $PROFILE }
function Reload-Profile { . $PROFILE }
function .. { Set-Location .. }
function ... { Set-Location ..\.. }
function touch($file) { New-Item -Path $file -ItemType File }
function Get-Port {
param([int]$port)
Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue | Select-Object LocalPort, OwningProcess
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。