请使用 Windwos 操作系统测试本文涉及的命令。
探索 Powershell
Get-Verb
Returns a list of verbs that most commands adhere to.Get-command
retrieves a list of all commands installed on your machine.Get-Member
operates on object based output and is able to discover what object, properties and methods are available for a command.Get-Help
displays a help page describing various parts of a command.Get-ChildItem
Gets the items and child items in one or more specified locations. (likels
for Unix)$Env:<variable-name>
display and change the values of environment variables, for example$Env:APPDATA
, `$Env:FOO = 'bar'setx
Creates or modifies environment variables in the user or system environment
常用命令
Install-Module posh-git -Scope CurrentUser -Force
- Install a module by nameUninstall-Module -Name oh-my-posh
- Uninstall a module by nameGet-InstalledModule -Name oh-my-posh | Uninstall-Module
- Use the pipeline to uninstall a moduledisplay all environment variable
Get-ChildItem -Path Env:
dir Env:
ls Env:
setx Path "$Env:Path;C:\Program Files\nodejs\node.exe"
permanently add node to the environment variable of the Path[guid]::NewGuid()
generate a Universally Unique Identifier
配置 PowerShell
配置过程分两部分,基础配置和自定义配置两部分。在基础配置中我们会创建一些必要的 PowerShell 配置文件,包括 PowerShell 当前用户配置和自定义配置两个文件,而在自定义配置阶段,我们将会安装一些必要的插件以及编写 PowerShell 自定义配置文件。
注意:PowerShell 版本必须在 7.2 及以上
基础配置
安装 PowerShell,微软商店入口
安装 scoop - A command-line installer for Windows
打开一个 PowerShell,运行命令
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # Optional: Needed to run a remote script the first time
irm get.scoop.sh | iex安装 Winget - Install and manage applications
安装 git for windows - Offer a lightweight, native set of tools
winget install -e --id Git.Git
安装
gcc
、neovim
scoop install gcc neovim
创建 PowerShell 配置文件
创建
$PROFILE.CurrentUserCurrentHost
文件,生成的文件路径为$Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
# Create a profile
New-Item -Path $PROFILE.CurrentUserCurrentHost -ItemType "file" -Force
# or use notepad commond
notepad $PROFILE创建 PowerSehll 自定义配置文件
user_profile.ps1
# $Home\Documents\PowerShell 获取文件的上级路径
Split-Path $PROFILE.CurrentUserCurrentHOST
mkdir $Home\.config\powershell
New-Item $Home\.config\powershell\user_profile.ps1要使自定义配置文件生效需要在
$PROFILE.CurrentUserCurrentHost
文件中添加以下内容,并重新启动终端。# $Home\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
. $env:USEPROFILE\.config\powershell\user_profile.ps1The
$PROFILE
variable 👉 about_profilesThis variable stores the path to the "Current User, Current Host" profile. The other profiles are saved in note properties of the
$PROFILE
variable. For example:host-specific profiles variable Current User, Current Host $PROFILE
Current User, Current Host $PROFILE.CurrentUserCurrentHost
Current User, All Hosts $PROFILE.CurrentUserAllHosts
`` All Users, Current Host $PROFILE.AllUsersCurrentHost
All Users, All Hosts $PROFILE.AllUsersAllHosts
以上所有的操作成功后,表示基础的配置准备已经完成,接下来进入安装插件阶段。
自定义配置(安装插件)
配置 prompt,下载 oh-my-posh (A prompt theme engine for any shell),安装步骤可以参考 oh-my-posh 官网安装教程
- Prompt for Git repositories
打开 PowerShell 终端,输入命令
winget install JanDeDobbeleer.OhMyPosh -s winget
# or
scoop install https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json安装完成后,重启你的 PowerShell 终端,开始配置 prompt 主题。编辑之前创建的自定义配置文件,终端输入下面命令可打开此文件
notepad $Home\.config\powershell\user_profile.ps1
然后在配置文件中键入以下内容,开启主题的使用。Oh My Posh 内建了许多开箱即用的主题,这里我选择 robbyrussel 主题。
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\robbyrussel.omp.json" | Invoke-Expression
重启 PowerShell 终端,查看 robbyrussel 主题是否生效。
你也可以自定义主题样式,详细步骤请参考Customize
安装 posh-git
posh-git 是一个 PowerShell 模块,整合 git 和 PowerShell,提供 git 状态信息,此状态信息可以展示在终端 prompt 中,也提供 tab 键自动补全 git 命令、分支名称和路径等支持。安装步骤参考posh-git installation,简单步骤如下:
# Installing posh-git via PowerShellGet
PowerShellGet\Install-Module posh-git -Scope CurrentUser -Force
# or Installing posh-git via Chocolatey
choco install poshgit
# or Installing posh-git via Scoop
scoop bucket add extras
scoop install posh-git在自定义配置文件中添加一行,重启终端即可生效。
# user_profile.ps1
Import-Module posh-git一个展示文件和文件夹的图标的 PowerShell 模块。
此模块必须依赖 Nerd Fonts 字体,在 download 页面选择一款字体,下载并安装字体。
在终端管理员模式下输入命令安装 Terminal-Icons 模块
# Install-Module
Install-Module -Name Terminal-Icons -Repository PSGallery -Force
# or use scoop
scoop bucket add extras
scoop install terminal-icons添加下面的内容到自定义配置文件
# user_profile.ps1
Import-Module Terminal-Icons安装 z
基于你的 cd 命令历史,z 让你在 PowerShell 中快速导航文件系统。在终端管理员模式下输入命令安装 z 模块
Install-Module -Name z -Force -AllowClobber
配置自定义文件,重启终端
# user_profile.ps1
Import-Module z安装 PSReadLine - autocompletion
Install-Module -Name PowerShellGet -Force
Install-Module -Name PSReadLine -Force -SkipPublisherCheckTo start using, just import the module:
Import-Module PSReadLine
To view the current key bindings:
Get-PSReadLineKeyHandler
To use Emacs key bindings, you can use:
Set-PSReadLineOption -EditMode Emacs
Specifies the source for PSReadLine to get predictive suggestions.
Set-PSReadLineOption -PredictionSource History
Sets the style for the display of the predictive text. The default is ListView.
Set-PSReadLineOption -PredictionViewStyle
InlineView# user_profile.ps1
Import-Module PSReadLine
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -PredictionSource HisoryAndPlugin
Set-PSReadLineOption -PredictionViewStyle ListView安装
fzf
PSFzfPSFzf is a PowerShell module that wraps fzf, a fuzzy file finder for the command line. Failed to import module
# 安装 fzf
scoop install fzf
# 安装 PSFzf
Install-Module -Name PSFzf -RequiredVersion 2.0.0 -Scope CurrentUser -Force自定义文件添加下面的内容
Import-Module PSFzf
Set-PsFzfOption -PSReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'重启配置文件
. $PROFILE
user_profile.ps1 自定义配置文件
Import-Module posh-git
Import-Module Terminal-Icons
Import-Module z
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\robbyrussel.omp.json" | Invoke-Expression
# PSReadLine
Import-Module PSReadLine
Set-PSReadLineOption -EditMode Emacs
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle InlineView
# fzf
Import-Module PSFzf
Set-PsFzfOption -PSReadLineChordProvider 'Ctrl+f' -PSReadLineChordReverseHistory 'Ctrl+r'
# Alias
Set-Alias ll ls
Set-Alias c clear
Set-Alias less 'C:\Program Files\Git\usr\bin\less.exe'
Set-Alias tig 'C:\Program Files\Git\usr\bin\tig.exe'
function which($command) {
Get-command -Name $command -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty Path -ErrorAction SilentlyContinue
}
资源
about_Environment_Provider The environment Environment provider lets you get, add, change, clear, delete environment variables and values in Powershell.
Windows and Powershell use Environment variables to store persistent information that affect system and process execution.
Unlike PowerShell variables, environment variables are not subject to scope constraints.
The Environment provider supports the following cmdlets.
about_Environment_Variables Environment variables store information about the operating system environment.
The environment variables store data that is used by the operating system and other programs.
about_CommonParameters 描述可与任何 cmdlet 一起使用的参数。