liangbm3's blog

Back

1. 前言#

记录在一次Windows中升级PowerShell 5.1至PowerShell 7的,并使用 oh my posh 对终端进行美化的折腾过程。

2. 升级PowerShell#

参考在 Windows 上安装 PowerShell - PowerShell | Microsoft Learn,在PowerShell中直接运行如下命令安装最新版本

winget install --id Microsoft.Powershell --source winget
powershell

安装完成后需要修改默认终端,随便打开一个终端,点击设置,然后将默认启动改为PowerShell 7,如图:

图片

3. 使用 oh my posh 进行美化#

在PowerShell中进行安装:

Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://ohmyposh.dev/install.ps1'))
powershell

重启终端,输入oh-my-posh,若打印如下内容,则说明安装成功:

图片

更改PowerShell的安全策略:以管理员身份运行,然后输入如下内容:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
powershell

打开配置文件

notepad $PROFILE
powershell

如果提示该文件不存在,可以使用如下命令创建

New-Item -Path $PROFILE -Type File -Force
powershell

在打开的配置文件添加以下代码并保存

oh-my-posh init pwsh | Invoke-Expression
plaintext

重新打开终端会加载默认主题。

Nerd Fonts 官网下载一款自己喜欢的字体,这里以Cousine Nerd Font为例,解压压缩包后,右键安装所有字体

图片

然后在外观中配置刚刚下载的字体,如图

图片

显示可用的主题:

Get-PoshThemes
powershell

记住好自己要切换的主题名称,然后ctrl+左键打开主题文件夹,如图

图片

打开配置文件:

notepad $PROFILE 
powershell

配置主题路径,以wholespace主题为例:

oh-my-posh init pwsh  --config "Senv:POSH_THEMES_PATH/wholespace.omp.json" | Invoke-Expression
plaintext

重载主题,命令如下

. $PROFILE
powershell

当然,还可以在终端外观设置中设置背景和透明度。

4. Vsocde中的乱码配置#

使用的时候发现在VScode中会乱码,安装下面步骤更改终端字体即可:

  1. 打开设置
  2. 搜索Integrated:Font Family
  3. 将字体修改为上面设置的字体

5. 参考资料#

https://mp.weixin.qq.com/s/KWlN5s_rdLn8oO_iL2vbgg

Windows 的 PowerShell 美化教程
https://liangbm3.site/blog/windows-de-powershell-mei-hua-jiao-cheng
Author liangbm3
Published at 2024年11月23日
Comment seems to stuck. Try to refresh?✨