Powershell美化

PWSH 5升级到PWSH 7

准备工作

电脑自带的是pwsh5.1,而pwsh7支持跨平台,并且加入了三元运算符和管道链运算符,报错内容更加简洁明了,而不是像5一样长篇大论。

可以使用$PSVersionTable查看一下版本,如果是7则可跳过此步。

推荐使用windows-terminal来使用多个shell,win11-24H2出厂自带,若系统并未安装该软件,可参考 微软官网安装指导

安装powershell7

在powershell5运行: winget install --id Microsoft.Powershell --source winget

安装完成后,重启windows terminal(win+r输入wt即可,以下简称wt)打开powershell 7,为当前用户创建一个配置文件(后面要用到):

1
if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }

pwsh7和pwsh5.1是共存的,因为他们的安装路径和可执行文件名,以及配置文件都是互相独立的,因此在wt上可以看到两个powershell。

7是安装在$env:ProgramFiles\PowerShell\7

而5.1安装在$env:WINDIR\System32\WindowsPowerShell\v1.0

对应的,他们的配置文件分别在$HOME\Documents\PowerShell$HOME\Documents\WindowsPowerShell

如果重启powershell,出现如下报错:

1
2
3
4
5
6
7
因为在此系统上禁止运行脚本有关详细信息请参阅   
https:/go.microsoft.com/fwlink/?LinkID=135170 中的about_Execution_Policies  
所在位置 :1 字符: 1  
+ hexo new "PowerShell:因为在此系统上禁止运行脚本,解决方法"  
+  
    + CategoryInfo          : SecurityError: (:) []PSSecurityException  
    + FullyQualifiedErrorId : UnauthorizedAccess

可以先检查下powershell执行策略的设置:get-executionpolicy

  • Restricted 执行策略不允许任何脚本运行。  
  • AllSigned 和 RemoteSigned 执行策略可防止 Windows PowerShell 运行没有数字签名的脚本。

如果是Restricted,以管理员身份打开 PowerShell 输入 set-executionpolicy remotesigned,接着重启powershell即可。

powershell7升级

使用wt打开powershell,提示升级powershell image.png

1、检查 $PSHOME 变量的值 得到C:\Program Files\PowerShell\7,说明是使用winget包或MSI包安装的ps7

其他结果说明:

  • 如果值为 $HOME\.dotnet\tools,则 PowerShell 随  .NET Global 工具 一起安装。
  • 如果值以 $Env:ProgramFiles\WindowsApps\ 开头,则 PowerShell 在使用 ARM 处理器的计算机上安装为  Microsoft Store 包 或与  Winget  一起安装。
  • 如果值是任何其他值,则可能是 PowerShell 安装为  ZIP 包

2、查看winget可用升级 winget list --name PowerShell --upgrade-available 得到如下信息: 名称 ID 版本 可用 源 PowerShell 7-x64 Microsoft.PowerShell 7.3.6.0 7.4.2.0 winget 1 升级可用。

3、使用winget升级,命令和安装是一样的 winget install --id Microsoft.Powershell --source winget

winget的包下载很慢,pws7的包就在github上,解决办法,执行安装命令后看到下载地址,手动卸载安装包后放入到执行目录中,然后重新执行命令便可使用自己下载的安装包进行安装,

执行目录位于C:\Users\用户名\AppData\Local\Temp\WinGet\Microsoft.PowerShell.7.4.2.0

或者给pwsh配置代理,notepad $PROFILE修改pwsh的配置文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function proxy
{
    $env:HTTP_PROXY="http://xxxx:xxxx"  #这里修改为自己的代理服务器及端口
    $env:HTTPS_PROXY="http://xxxx:xxxx"  #这里修改为自己的代理服务器及端口,并且注意还是http
    echo "HTTP&HTTPS Proxy on!"
}

function noproxy 
{
    $env:HTTP_PROXY=""
    $env:HTTPS_PROXY=""
    echo "HTTP&HTTPS Proxy off!"
}

配置完成后,使用curl测试一下curl.exe -I http://www.google.com

或者配置winget的代理(推荐): 注意,配置前需要先开启winget settings --enable ProxyCommandLineOptions

1
winget install --proxy your_proxy_ip:your_port xxxx --source winget

安装oh-my-posh

官方GitHub 仓库 ,类似于oh-my-zsh,为pwsh提供了很多主题

  1. 安装oh-my-posh: winget install JanDeDobbeleer.OhMyPosh -s winget,待安装后重启pwsh,输入(Get-Command oh-my-posh).Source验证安装是否成功

  2. 修改pwsh配置文件 oh-my-posh自带了很多主题,主题的配置文件使用json格式,存放的目录可以通过$env:POSH_THEMES_PATH找到,可以到 官网 预览各种主题的样式,然后选择一款主题,写入pwsh的配置文件中。

    notepad $PROFILE 打开配置文件,选择你想要的主题,格式如下:

1
oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\powerlevel10k_rainbow.omp.json" | Invoke-Expression

其中的powerlevel10k_rainbow.omp.json 就是主题的json文件,可根据自己喜好更改。

保存配置文件后,. $PROFILE 来使配置文件生效。

  1. 主题生效后,可能会有一些图形图案无法显示,表现为矩形框框,这是因为字体不适配的原因,oh-my-posh官方推荐下载 Nerd Fonts 字体,下载安装完字体后,在pwsh中使用Ctrl+Shift+, 来打开pwsh的setting.json文件,这里类似于主题文件,对pwsh的字体、背景、快捷键等进行了配置。

    找到font的位置,将face的值改为Nerd的字体。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
   {
    "profiles":
    {
        "defaults":
        {
            "font":
            {
                "face": "MesloLGM NF"
            }
        }
    }
    }
  1. oh-my-posh还有一个很好用的功能,就是可以补全历史命令,pwsh也可以,需要安装 PSReadLine ,令人高兴的是,pwsh7中已经内置了PSReadLine,只需要在配置文件中设置即可。

    notepad $PROFILE 打开配置文件,在文件尾部增加Set-PSReadLineOption -PredictionSource History

更多的具体细节配置可参考 官网

更新oh-my-posh

自动更新(每次打开pwsh自动检查更新): oh-my-posh enable upgrade

手动更新: winget upgrade JanDeDobbeleer.OhMyPosh -s winget

pwsh启动时间久问题

WT启动pwsh时,动不动四五s,Loading personal and system profiles took 5726ms.

有点小烦说是,查看了下本地启动配置文件,发现存在2个文件:

Microsoft.PowerShell_profile.ps1profile.ps1

其中profile.ps1中存在conda的初始化命令,是由于之前为了在powshell中使用conda,运行了conda init powershell导致的。初始化的具体内容如下:

1
2
3
4
5
6
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "E:\anaconda3\Scripts\conda.exe") {
    (& "E:\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion

将内容删除,或者直接将该文件删除,使用Microsoft.PowerShell_profile.ps1作为配置文件。

再次启动pwsh:Loading personal and system profiles took 2071ms.少了3s说是。配置文件中只有OMP的主题初始化脚本了,怀疑是oh-my-posh的问题了。但是测试不同电脑似乎启动速度不同,家里PC不到1s启动完成,而笔记本则要2s。可能和电脑配置有关。

PWSH7.5.2版本与conda冲突问题

250723更新PWSH到V7.5.2后,启动pwsh时报错:

1
usage: conda-script.py [-h] [–no-plugins] [-V] COMMAND … conda-script.py: error: argument COMMAND: invalid choice: ‘activate’ (choose from ‘clean’, ‘compare’, ‘config’, ‘create’, ‘info’, ‘init’, ‘install’, ‘list’, ‘notices’, ‘package’, ‘remove’, ‘uninstall’, ‘rename’, ‘run’, ‘search’, ‘update’, ‘upgrade’, ‘build’, ‘convert’, ‘debug’, ‘develop’, ‘doctor’, ‘index’, ‘inspect’, ‘metapackage’, ‘render’, ‘skeleton’, ‘token’, ‘repo’, ‘server’, ‘pack’, ‘env’, ‘verify’, ‘content-trust’)

在conda的 github 上找到解决方案:更新conda到V25.1.1+conda官网 查看现在windows最新版本为25.5.1 在Anaconda Prompt中更新

1
2
conda activate base
conda install -n base -c defaults conda=25.5.1