首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >PowerShell 7.x 的自定义设置

PowerShell 7.x 的自定义设置

原创
作者头像
保持热爱奔赴山海
发布2026-04-15 09:36:13
发布2026-04-15 09:36:13
320
举报
文章被收录于专栏:DevOpsDevOps

安装高版本的powershell

代码语言:txt
复制
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.x

安装 PowerShell 扩展模块

代码语言:txt
复制
Install-Module PSReadLine -Force -SkipPublisherCheck
Install-Module Terminal-Icons -Force
Install-Module posh-git -Force

安装OhMyPosh

代码语言:txt
复制
推荐使用winget安装,命令如下:
winget install JanDeDobbeleer.OhMyPosh --source winget

安装 Nerd Font

Oh My Posh 大量使用图标字符,需要安装 Nerd Font 才能正确显示。

安装完成后,在终端设置中将字体改为对应的 Nerd Font(如 FiraCode Nerd Font、Hack Nerd Font 等)。

如果使用的是 Windows Terminal,打开 设置 → 配置文件 → 外观 → 字体,选择已安装的 Nerd Font。

代码语言:txt
复制
安装字体: 
oh-my-posh font install

在终端设置中将字体改为对应的 Nerd Font(如 FiraCode Nerd Font)

重启终端(不是新开标签页,是完全关闭再打开)

安装主题

代码语言:txt
复制
https://ohmyposh.dev/docs/themes/
选择一个喜欢的主题,将它下载到本地。例如我这里下载的是 aliens.omp.json ,放到了 C:\powershell_settings\aliens.omp.json 这个自己创建的目录下。

安装zoxide智能目录跳转工具

代码语言:txt
复制
winget install zoxide

powershell自定义配置

编辑配置文件 notepad $PROFILE 填入如下的内容

代码语言:txt
复制
# 我这里用了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 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装高版本的powershell
  • 安装 PowerShell 扩展模块
  • 安装OhMyPosh
  • 安装 Nerd Font
  • 安装主题
  • 安装zoxide智能目录跳转工具
  • powershell自定义配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档