目录
- 1. 启动 powershell
- 2. 查看 powershell 版本
- 3. 升级 powershell
- 4. 修改执行策略
- 5. powershell7 说明
- 6. 配置启动文件
- 7. oh-my-posh 下载安装
- 8. 配置 oh-my-posh Themes
- 9. 配置 Windows Terminal 主题
- 10. 其他自定义快捷方式
- 11. vscode 搭配
- 12. 最后
如果更换电脑,要配置 windows terminal 的话,本文恰好可以作为一个初步的参考。
1. 启动 powershell
其实比较简单,直接 win + S
搜索到 powershell 即可启动,win 11 我们直接使用默认的 windows terminal 来启动 powershell, 可以按下 Ctrl
打开新标签页,默认开启管理员模式
2. 查看 powershell 版本
$PSVersionTable
Name Value
---- -----
PSVersion 7.4.0-preview.2
PSEdition Core
GitCommitId 7.4.0-preview.2
OS Microsoft Windows 10.0.22621
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
3. 升级 powershell
如果是 powershell5.1, 可以升级一下到 7, 有些新特性支持,配置文件的方法也改变了一些
winget search Microsoft.PowerShell
winget install --id Microsoft.Powershell --source winget
winget install --id Microsoft.Powershell.Preview --source winget
参考 这里 的最新命令,环境变量会自动添加到系统表中
4. 修改执行策略
具体原因不详,但是通过以下配置可以无障碍运行ps脚本
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution
policy might expose you to the security risks described in the about_Execution_Policies help topic
at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):y
5. powershell7 说明
PowerShell 7 是专为云、本地和混合环境设计的,它包含增强功能和新功能。
- 与 Windows PowerShell 并行安装和运行
- 提升了与现有 Windows PowerShell 模块的兼容性
- 新语言功能(如三元运算符和
ForEach-Object -Parallel
) - 提高了性能
- 基于 SSH 的远程处理
- 跨平台互操作性
- 支持 Docker 容器
注意: powershell 7 的配置路径和 5 不在一起,需要先明确它的配置路径
- 查看 PSModulePath 位置 (安装或自定义的一些插件位置)
$Env:PSModulePath -split (';')
C:\Users\yu123\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7-preview\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
PS C:\Users\yu123> $env:ProgramFiles\PowerShell\7
- 查看 Powershell 启动脚本位置 (主启动文件)
PS C:\Users\yu123> $PROFILE | Select-Object *Host* | Format-List
AllUsersAllHosts : C:\Program Files\PowerShell\7-preview\profile.ps1
AllUsersCurrentHost : C:\Program Files\PowerShell\7-preview\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\yu123\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\yu123\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
6. 配置启动文件
新建文件并使用 vscode 打开
New-Item -Path $Profile -Force
code $Profile
7. oh-my-posh 下载安装
Oh My Posh 安装说明-官网
我使用的是 Fira Code 字体: 下载 , 然后放入 C:\Windows\Fonts
文件夹下进行安装
【可选】选择带图标的 NerdFont 字体 下载
Get-InstalledModule
winget install JanDeDobbeleer.OhMyPosh -s winget
Install-Module posh-git -Scope CurrentUser
Install-Module npm-completion -Scope CurrentUser
在前面打开的启动文件中,填入下面的配置:
oh-my-posh init pwsh | Invoke-Expression
Import-Module posh-git
Import-Module npm-completion
Set-PSReadLineKeyHandler -Chord "Tab" -Function MenuComplete
Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord
提示:
- 推荐根据 oh-my-posh 的官网介绍来选择最新的安装方法和配置方法
- powershell 7自带 PSReadLine 不需要额外下载引入了
8. 配置 oh-my-posh Themes
搞半天不就为了这个嘛。
主题预览
主题默认已经全部下载到本地了,直接配置上就可以用了,这里选择了 ys 主题,你可以选择你喜欢的即可
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\ys.omp.json" | Invoke-Expression
9. 配置 Windows Terminal 主题
我选择了 JetBrains Darcula
这个主题,直接复制好颜色代码填入 schemes
中,然后在 "profiles": "defaults": { }
里面设置对应的主题。
{
"...": "省略不重要的一些配置",
"defaultProfile": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
+ "initialCols": 100,
+ "initialPosition": "1920,1080",
+ "initialRows": 30,
+ "centerOnLaunch": true,
+ "profiles": {
+ "defaults": {
+ "colorScheme": "JetBrains Darcula",
+ "cursorShape": "bar"
+ "cursorShape": "bar",
+ "elevate": false,
+ "startingDirectory": null
+ },
"list": [
{
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"name": "Windows PowerShell"
},
{
"guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
+ "font": { "face": "Fira Code", "size": 12.0 },
"hidden": false,
"name": "PowerShell",
"source": "Windows.Terminal.PowershellCore"
}
]
},
"schemes": [
+ {
+ "background": "#202020",
+ "black": "#000000",
+ "blue": "#4581EB",
+ "brightBlack": "#555555",
+ "brightBlue": "#6D9DF1",
+ "brightCyan": "#60D3D1",
+ "brightGreen": "#67FF4F",
+ "brightPurple": "#FB82FF",
+ "brightRed": "#FB7172",
+ "brightWhite": "#EEEEEE",
+ "brightYellow": "#FFFF00",
+ "cursorColor": "#FFFFFF",
+ "cyan": "#33C2C1",
+ "foreground": "#ADADAD",
+ "green": "#126E00",
+ "name": "JetBrains Darcula",
+ "purple": "#FA54FF",
+ "red": "#FA5355",
+ "selectionBackground": "#1A3272",
+ "white": "#ADADAD",
+ "yellow": "#C2C300"
+ },
],
}
10. 其他自定义快捷方式
基本上都弄好了,还可以在启动脚本中配置一些自己的自定义命令
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\ys.omp.json" | Invoke-Expression
Import-Module npm-completion
Import-Module posh-git
Set-PSReadLineKeyHandler -Chord "Tab" -Function MenuComplete
Set-PSReadLineKeyHandler -Chord "Ctrl+f" -Function ForwardWord
function CreateNewFile () {
param([string]$Path)
New-Item -Path $Path -ItemType File -Force
}
Set-Alias -Name touch -Value CreateNewFile
function ListDirectory {
(Get-ChildItem).Name
Write-Host("")
}
Set-Alias -Name ls -Value ListDirectory
Set-Alias -Name ll -Value Get-ChildItem
function OpenCurrentFolder {
param
(
$Path = '.'
)
Invoke-Item $Path
}
Set-Alias -Name open -Value OpenCurrentFolder
function findFileRecurse {
param($queryFile)
dir -Path '.\' -Filter $queryFile -Recurse
}
Set-Alias -Name find -Value findFileRecurse
function findStringRecurse {
param($queryString, $queryPath='*.*')
findstr /s /i $queryString $queryPath
}
Set-Alias -Name grep -Value findStringRecurse
function goToWorkspace { cd D:\giteeworkspace\repos }
Set-Alias -Name work -Value goToWorkspace
function generateSsh { ssh-keygen -t ed25519 -C "$args[0]" }
Set-Alias -Name gssh -Value generateSsh
set-alias -name pn -value pnpm
set-alias -name tsd -value ts-node
不会 powershell 的脚本语法,东拼西凑找了写了一些自己有用的,但是感觉这个东西还挺强的。
11. vscode 搭配
vscode 中的终端要应用如上的主题,需要修改一下它的配置文件:
{
"terminal.integrated.defaultLocation": "view",
"terminal.integrated.defaultProfile.windows": "pwsh",
"terminal.integrated.fontFamily": "Fira Code, MesloLGS Nerd Font Mono",
"terminal.integrated.rightClickBehavior": "default",
"terminal.integrated.cursorBlinking": true,
"terminal.integrated.shellIntegration.decorationsEnabled": "never",
"terminal.integrated.shellIntegration.suggestEnabled": true,
"terminal.integrated.fontSize": 13,
"terminal.integrated.cursorWidth": 2,
"terminal.integrated.cursorStyle": "line",
"terminal.integrated.enableMultiLinePasteWarning": false,
}
12. 最后
其实装了个主题也没有啥特别的效果,但是偶尔换个终端的主题,心情还是会好一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)