PowerShell
安装
下载并安装 PowerShell 7.x
创建配置文件
ps1
New-Item -Path $PROFILE -Type File -Force
常见指令
ps1
Install-Module -Name <模块名称> -Scope CurrentUser
# 安装名为PSReadLine的模块
Install-Module -Name PSReadLine -Scope CurrentUser
ps1
Update-Module -Name <模块名称> -Scope CurrentUser
# 升级名为PSReadLine的模块
Update-Module -Name PSReadLine -Scope CurrentUser
ps1
Uninstall-Module -Name <模块名称> -AllVersions -Scope CurrentUser
# 卸载名为PSReadLine的模块
Uninstall-Module -Name PSReadLine -AllVersions -Scope CurrentUser
ps1
# 查找所有模块
Find-Module
# 查看已安装模块
Get-InstalledModule | Select-Object Name, Version, Summary
常用参数说明
-Name
: 指定要操作的模块名称。-Scope
: 指定模块的安装范围。常用的选项有 CurrentUser(当前用户),AllUsers(所有用户)。-AllVersions
: 用于卸载时,指定是否删除该模块的所有版本。
插件
PowerShell 需要安装几个插件用于提升体验,安装方式如下:
ps1
# git 语法支持
Install-Module posh-git
# 命令行编辑体验增强
Install-Module PSReadLine -Force
oh-my-posh
下载并安装 ohMyPosh 安装工具 install-amd64.exe
ps1
# notepad $PROFILE
oh-my-posh init pwsh | Invoke-Expression
ps1
# notepad $PROFILE
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\robbyrussell.omp.json" | Invoke-Expression
配置案例
ps1
# 初始化 oh-my-posh 并载入主题
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\robbyrussell.omp.json" | Invoke-Expression
# 加载 post-git 模块
Import-Module posh-git
# 命令预测的来源设为历史记录
Set-PSReadLineOption -PredictionSource History
# 设置编辑器模式为Vi模式:
# Set-PSReadlineOption -EditMode Vi
# 如果想区分两种模式的光标
# Set-PSReadlineOption -ViModeIndicator Cursor
# Tab 按键兼容 oh-my-posh 和 Vi 模式
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete