Skip to Content
OSWindowsPowerShell profile

PowerShell profile 通用配置

这里放 oh-my-posh 和 Starship 都能共用的 Windows Terminal / PowerShell 配置。实际 prompt 只启用一个:使用 oh-my-posh 时加载 oh-my-posh,使用 Starship 时加载 Starship。

升级 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 启动

以管理员身份运行 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 'Invoke-Expression (& { (zoxide init powershell | Out-String) })' >> $PROFILE # fzf 模糊搜索工具,可以配合 zoxide 使用 scoop install fzf

自用别名 alias

# ============================================================ # 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 -> autojump j = 'z' # 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

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
Last updated on