背景

我的 windows terminal 每次启动 powershell 都很慢,并且会打印这样一行

1
Loading personal and system profiles took 1704ms.

排查

首先我们不加载任何内容,在 cmd 中执行 pwsh -NoProfile,powershell 立即启动,并且没有打印上面的日志

在 powershell 中执行 $PROFILE​,找到 profile 所在文件夹,然后找到同级目录下的 profile.ps1

我的内容如下

1
2
3
4
5
6
7
Import-Module -Name "F7History"
#region conda initialize
# !! Contents within this block are managed by 'conda init' !!
If (Test-Path "D:\Applications\Scoop\apps\miniconda\current\Scripts\conda.exe") {
    (& "D:\Applications\Scoop\apps\miniconda\current\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}
#endregion

注释掉 F7History,似乎没有改善

注释掉 conda 相关,启动 powershell,Loading personal and system profiles took 1704ms. 这一行没有打印了,并且感觉启动变快了。修改 profile 来记录一下

1
2
3
4
5
6
$global:ProfileStartTime = Get-Date

... 原有内容

$profileLoadTime = (Get-Date) - $global:ProfileStartTime
Write-Host "⚡ Profile 加载耗时: $($profileLoadTime.TotalMilliseconds.ToString('N0')) ms" -ForegroundColor Cyan

可以看到注释掉 conda,确实变快了

解决方案

  1. 注释掉 conda
  2. 或者你可以参考 conda init powershell slows shell startup immensely. · Issue #11648 · conda/conda 中的解决方案