Skip to Content
OSWindowsPowerShell profile

PowerShell profile 通用配置

这里放 oh-my-posh 和 Starship 都能共用的 Windows Terminal / PowerShell 配置。实际 prompt 只启用一个:使用 oh-my-posh 时加载 oh-my-posh,使用 Starship 时加载 Starship。为了优先 键入速度,zoxide init 直接在 profile 里同步执行,z / zi 立即可用;Starship 也建议走缓存 文件,避免每次启动都重新生成 init script。Node.js 版本管理单独看 fnm 文档

升级 PowerShell 版本(可选)

官方升级文档 

如果使用 Windows Terminal,升级后在 Windows Terminal 的设置里面把默认的 shell 改成升级后的 PowerShell。

配置 Terminal 字体

先安装 Nerd Font,例如 MesloLGM Nerd Font。如果已经通过 oh-my-posh 安装过 Meslo 字体, 这里可以直接跳过安装。

在 Terminal 配置文件里面设置字体,打开 Windows Terminal 的设置,找到 profiles -> defaults -> font -> face,把它改成下面的字体。

{ "profiles": { "defaults": { "font": { "face": "MesloLGM Nerd Font" } } } }

配置 vscode 的 json 文件,添加字体配置。

"terminal.integrated.fontFamily": "MesloLGM Nerd Font"

优化 PowerShell 启动

这是旧的 .NET/ngen 启动优化参考,现代 PowerShell / Windows Terminal 一般不建议默认执行。 如果你没有明确的启动瓶颈,不要把它放进日常配置。 以管理员身份运行 PowerShell,运行下面的命令。来源 

$env:PATH = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory() [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $path = $_.Location if ($path) { $name = Split-Path $path -Leaf Write-Host -ForegroundColor Yellow "`r`nRunning ngen.exe on '$name'" ngen.exe install $path /nologo } }

模块

# ReadLine 命令自动补全 Install-Module PSReadLine -Force
# zoxide 目录跳转工具,类似于 autojump scoop install zoxide # fzf 模糊搜索工具,可以配合 zoxide 使用 scoop install fzf

默认 profile

默认 profile 只保留 Starship、zoxide 和 alias。Python venv、fnm 这类环境相关配置按需追加。

# ============================================================ # Fast startup helpers # ============================================================ if (Get-Command zoxide -CommandType Application -ErrorAction SilentlyContinue) { Invoke-Expression (& { (zoxide init powershell | Out-String) }) } else { Write-Error "zoxide executable was not found in PATH." } function Initialize-Starship { $cacheRoot = Join-Path $env:LOCALAPPDATA "starship" $cachePath = Join-Path $cacheRoot "init.cached.ps1" if (-not (Test-Path -LiteralPath $cacheRoot -PathType Container)) { New-Item -ItemType Directory -Path $cacheRoot -Force | Out-Null } if (-not (Test-Path -LiteralPath $cachePath -PathType Leaf)) { starship init powershell | Out-File -LiteralPath $cachePath -Encoding utf8 } . $cachePath } # ============================================================ # Aliases # ============================================================ $aliases = [ordered]@{ # Scoop s = 'scoop' # Git g = 'git' gs = 'git stash' gct = 'git commit' gr = 'git reset --soft HEAD~1' gaa = 'git add .' gch = 'git checkout' # zoxide j = 'z' ji = 'zi' # Editor c = 'code .' # Bun br = 'bun run' bx = 'bunx' # Python uvpy = 'uv run python' uvpip = 'uv pip' py = 'python' # System ll = 'Get-ChildItem -Force' la = 'Get-ChildItem -Force -Hidden' # Tools ard = 'aria2c --summary-interval=10 -x 3 --allow-overwrite=true -Z' } foreach ($alias in $aliases.GetEnumerator()) { Set-Item -LiteralPath "Function:global:$($alias.Key)" -Value "& $($alias.Value) @args" } Remove-Variable aliases Initialize-Starship

可选插槽

Python venv

激活当前目录或指定路径下的 Python virtual environment。

function Activate-Venv { param( [string]$Path = ".venv" ) $candidates = @( $Path, (Join-Path $Path "Scripts\Activate.ps1"), (Join-Path $Path ".venv\Scripts\Activate.ps1") ) $activate = $candidates | Where-Object { Test-Path -LiteralPath $_ -PathType Leaf } | Select-Object -First 1 if (-not $activate) { Write-Error "No PowerShell virtual environment activation script found for '$Path'." return } Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force . $activate } Set-Alias -Name venv -Value Activate-Venv Set-Alias -Name va -Value Activate-Venv function Deactivate-Venv { if (Get-Command deactivate -ErrorAction SilentlyContinue) { deactivate return } Write-Host "No active Python virtual environment." } Set-Alias -Name vde -Value Deactivate-Venv

fnm

PowerShell 的 fnm 初始化放在 Node 版本管理

Last updated on