676 字
3 分钟
修复 Conda 导致的 PowerShell 启动缓慢
问题背景
最近发现我的 PowerShell 启动异常缓慢,从正常的瞬间启动延长到了 5~7 秒。作为经常要使用到的使用终端,每次启动都要被硬控好几秒不能使用,这严重影响了效率。
问题诊断过程
性能测量
- 首先使用
Measure-Command
测试正常启动时间:
Measure-Command { pwsh.exe -Command "exit" }
PS C:\Users\Alaye> Measure-Command { powershell -Command "exit" }
Days : 0Hours : 0Minutes : 0Seconds : 5Milliseconds : 859Ticks : 58595344TotalDays : 6.78186851851852E-05TotalHours : 0.00162764844444444TotalMinutes : 0.0976589066666667TotalSeconds : 5.8595344TotalMilliseconds : 5859.5344
5s
- 测试无配置文件启动时间:
Measure-Command { powershell -NoProfile -Command "exit" }
PS C:\Users\Alaye> Measure-Command { powershell -NoProfile -Command "exit" }
Days : 0Hours : 0Minutes : 0Seconds : 0Milliseconds : 217Ticks : 2171056TotalDays : 2.5127962962963E-06TotalHours : 6.03071111111111E-05TotalMinutes : 0.00361842666666667TotalSeconds : 0.2171056TotalMilliseconds : 217.1056
0.22s
对比正常启动时间和无配置文件启动时间,发现相差过大,则可以判断为是启动配置影响了启动时间。
检查 PowerShell 配置文件
检查配置文件位置:
$PROFILE | Get-Member -Type NoteProperty
PS C:\Users\Alaye> $PROFILE | Get-Member -Type NoteProperty
TypeName: System.String
Name MemberType Definition---- ---------- ----------AllUsersAllHosts NoteProperty string AllUsersAllHosts=C:\Program Files\PowerShell\7\profile.ps1AllUsersCurrentHost NoteProperty string AllUsersCurrentHost=C:\Program Files\PowerShell\7\Microsoft.PowerShell_prof…CurrentUserAllHosts NoteProperty string CurrentUserAllHosts=C:\Users\Alaye\Documents\PowerShell\profile.ps1CurrentUserCurrentHost NoteProperty string CurrentUserCurrentHost=C:\Users\Alaye\Documents\PowerShell\Microsoft.PowerS…
检查配置文件内容来定位问题,可以查看对应路径的内容或者使用命令行:
$PROFILE | Get-Member -Type NoteProperty | ForEach-Object { $path = $PROFILE.($_.Name) Write-Host "`n=== $($_.Name) ===" -ForegroundColor Yellow Write-Host "路径: $path" if (Test-Path $path) { Write-Host "内容:" Get-Content $path | ForEach-Object { Write-Host " $_" } } else { Write-Host "文件不存在" }}
发现问题根源
在 CurrentUserAllHosts
配置文件中发现了 Conda 初始化代码:
# region conda initialize# !! Contents within this block are managed by 'conda init' !!If (Test-Path "D:\DevEnv\anaconda3\Scripts\conda.exe") { (& "D:\DevEnv\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression}#endregion
根本原因:PowerShell 启动时会尝试执行该初始化脚本,导致长时间等待。
解决方案
实现 Conda 延迟加载
为了在保持快速启动的同时仍能使用 Conda,实现延迟加载机制,修改配置文件:
# 简单的 Conda 延迟加载function Invoke-CondaInit { if (-not $script:CondaAlreadyInitialized) { Write-Host "正在初始化 Conda..." -ForegroundColor Yellow If (Test-Path "D:\DevEnv\anaconda3\Scripts\conda.exe") { (& "D:\DevEnv\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Where-Object {$_} | Invoke-Expression } $script:CondaAlreadyInitialized = $true }}
# 只有在使用 conda 相关命令时才初始化Set-Alias -Name conda -Value conda-wrapperfunction conda-wrapper { Invoke-CondaInit conda @args}
性能优化效果
- 优化前:>5 秒
- 使用延迟加载:启动<1 秒,首次使用 conda 时延迟加载 (约 2-3 秒)
总结
现在的配置方案既能保证 PowerShell 快速启动,又能在需要时使用 Conda 环境。
关键原则:保持启动脚本的轻量化,将重量级功能改为按需加载,这是提升命令行启动响应速度的有效方法。
修复 Conda 导致的 PowerShell 启动缓慢
https://blog.alayedong.cn/posts/2025/fixing-slow-powershell-startup-caused-by-conda/