PowerShell 启动慢问题排查与解决

问题现象

  • Windows 11 下打开 PowerShell 需要等待约 2 秒
  • 提示信息:加载个人及系统配置文件用了 2103 毫秒
  • 同时出现屏幕阅读器警告

排查过程

第一步:确认是否为配置文件问题

powershell -NoProfile
  • 加上 -NoProfile 参数后启动速度飞快
  • 结论:问题出在配置文件中

第二步:检查个人配置文件

notepad $PROFILE
  • 发现文件为空
  • 结论:问题不在默认的个人配置文件中

第三步:检查系统级配置文件

$PROFILE.AllUsersAllHosts
Test-Path $PROFILE.AllUsersAllHosts
  • 路径:C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
  • 文件不存在
  • 结论:问题也不在系统级配置文件中

第四步:使用计时脚本逐行分析

$profilePath = $PROFILE.CurrentUserAllHosts
Get-Content $profilePath | ForEach-Object {
    $line = $_
    if ($line.Trim() -eq '' -or $line.Trim().StartsWith('#')) {
        Write-Host "跳过: $line" -ForegroundColor Gray
    } else {
        $time = Measure-Command { Invoke-Expression $line 2>$null }
        $ms = $time.TotalMilliseconds
        if ($ms -gt 500) {
            Write-Host "⚠️  [$ms.ToString('F0')ms] $line" -ForegroundColor Red
        } elseif ($ms -gt 100) {
            Write-Host "⚡ [$ms.ToString('F0')ms] $line" -ForegroundColor Yellow
        } else {
            Write-Host "✓  [$ms.ToString('F0')ms] $line" -ForegroundColor Green
        }
    }
}

发现结果

  • 有一段 Anaconda 初始化代码耗时约 2012 毫秒
  • 代码出现在非默认位置的配置文件中

第五步:定位真正的配置文件

conda init --dry-run --verbose powershell
  • 最后一行显示:no change D:\Backup\Documents\WindowsPowerShell\profile.ps1
  • 结论:Anaconda 将启动脚本写入了这个非默认位置

第六步:查看脚本内容

Get-Content "D:\Backup\Documents\WindowsPowerShell\profile.ps1"

问题根源

脚本内容

If (Test-Path "G:\ProgramFiles\anaconda3\Scripts\conda.exe") {
    (& "G:\ProgramFiles\anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | ?{$_} | Invoke-Expression
}

脚本作用逐句解释

代码含义
Test-Path "G:\...\conda.exe"检查 conda.exe 是否存在
& "G:\...\conda.exe" "shell.powershell" "hook"运行 conda.exe 输出 PowerShell 代码
Out-String将输出转换为字符串
?{$_}过滤空行(?Where-Object 的简写)
Invoke-Expression执行生成的代码(设置环境变量、定义函数等)

产生原因

  • 安装 Anaconda 或 PyCharm 时 PowerShell 报错(可能是执行策略限制)
  • 按照网上的解决方法,手动创建了 profile.ps1 文件并粘贴了上述代码
  • 这个脚本虽然让 conda 能正常工作,但导致每次 PowerShell 启动都需等待约 2 秒

解决方案

执行命令

Remove-Item "D:\Backup\Documents\WindowsPowerShell\profile.ps1" -Force

重启 PowerShell 验证

  • 启动速度恢复正常(<0.1 秒)

后续使用 conda 的方法

方法一:手动激活(推荐)

conda activate base

方法二:完整初始化命令

& "G:\ProgramFiles\anaconda3\Scripts\conda.exe" "shell.powershell" "hook" | Out-String | Invoke-Expression

方法三:创建别名(在 $PROFILE 中添加)

function conda { & "G:\ProgramFiles\anaconda3\Scripts\conda.exe" @args }
function ActivateConda {
    & "G:\ProgramFiles\anaconda3\Scripts\conda.exe" "shell.powershell" "hook" | Out-String | Invoke-Expression
    Write-Host "Conda 环境已激活" -ForegroundColor Green
}

经验总结

为什么问题难以定位

  1. Anaconda 将脚本写入了非默认位置:D:\Backup\Documents\WindowsPowerShell\profile.ps1
  2. 默认的 $PROFILE 指向 C:\Users\...\Documents\WindowsPowerShell\profile.ps1(空文件)
  3. 网上常见的解决方法虽然能用,但引入了性能问题

PowerShell 配置文件搜索顺序

优先级变量路径作用域
1$PROFILE.CurrentUserCurrentHost~\Documents\WindowsPowerShell\profile.ps1当前用户、当前主机
2$PROFILE.CurrentUserAllHosts~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1当前用户、所有主机
3$PROFILE.AllUsersCurrentHost$PSHOME\profile.ps1所有用户、当前主机
4$PROFILE.AllUsersAllHosts$PSHOME\Microsoft.PowerShell_profile.ps1所有用户、所有主机

关键经验

  • 使用 -NoProfile 可以快速判断是否为配置文件导致的问题
  • $PROFILE 默认指向用户级配置,但系统可能还存在其他位置的配置文件
  • 手动创建的脚本虽然解决了当时的问题,但可能带来性能损耗

相关命令速查

# 查看所有配置文件路径
$PROFILE | Get-Member -Type NoteProperty

# 跳过配置文件启动
powershell -NoProfile

# 查看 conda 注册信息
conda init --dry-run --verbose powershell

# 移除 conda 对 PowerShell 的注册
conda init --reverse powershell

# 清除 PowerShell 缓存
Remove-Item -Path "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\ModuleAnalysisCache" -Force -ErrorAction SilentlyContinue

解决结果

✅ PowerShell 启动速度从 2103 毫秒降至 <100 毫秒
✅ 屏幕阅读器警告依然存在(可另行解决,不影响使用)
✅ conda 功能可在需要时手动激活使用

标签: none

添加新评论