Powershell 在 Windows 10 上启动缓慢

mik*_*ike 13 performance powershell windows-10

我在 Windows 10(版本 1703 - Creators Update)上遇到 powershell 提示启动缓慢的问题。

我的硬件规格(相当快的机器):英特尔 i5-7440HQ(四核)/32GB DDR4 RAM/512 三星 SSD 硬盘。

我试图绕过配置文件和执行策略,但它没有改变任何东西:

powershell -noprofile -ExecutionPolicy Bypass ( Measure-Command { powershell "Write-Host 1" } ).TotalSeconds

6,228067

我的朋友在没有 Creators Update 的情况下使用 Windows 10 的同一台笔记本电脑在不到 0.5 秒的时间内运行了 powershell。

还尝试使用 ngen.exe 进行一些编译,但没有帮助:

$env:path = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
[AppDomain]::CurrentDomain.GetAssemblies() | % {
  if (! $_.location) {continue}
  $Name = Split-Path $_.location -leaf
  Write-Host -ForegroundColor Yellow "NGENing : $Name"
  ngen install $_.location | % {"`t$_"}
}
Run Code Online (Sandbox Code Playgroud)

知道我该如何调查这个问题吗?

你好

Sli*_*SFT 12

这也发生在我身上 - 尽管可能不是最佳途径,但添加到Windows Defender 排除powershell.exe列表中可以将速度从 20 秒加快到 < 1 秒。

使用旧版控制台、清除 PSReadLine 和运行 ngen 似乎根本没有帮助。

  • 完美的!这成功了。我遇到了与您类似的问题,窗口会打开,但路径加载时间不会超过 30 秒。一旦我将其从 Defender 中排除,它就会立即加载。多谢! (2认同)

小智 6

我已经遇到同样的问题相当长一段时间了,直到 PowerShell 启动失败并出现以下错误:

Exception:
System.OutOfMemoryException: Array dimensions exceeded supported range.
   at System.Collections.Generic.List`1.set_Capacity(Int32 value)
   at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
   at System.Collections.Generic.List`1.Add(T item)
   at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
   at Microsoft.PowerShell.PSConsoleReadLine.<ReadHistoryFile>b__67_0()
   at Microsoft.PowerShell.PSConsoleReadLine.WithHistoryFileMutexDo(Int32 timeout, Action action)
   at Microsoft.PowerShell.PSConsoleReadLine.DelayedOneTimeInitialize()
   at Microsoft.PowerShell.PSConsoleReadLine.Initialize(Runspace runspace, EngineIntrinsics engineIntrinsics)
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
-----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

这让我发现了现有的 Github 问题:https ://github.com/Powershell/PSReadLine/issues/673

我尝试删除当时~\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\超过 6 GB 的历史记录文件,之后 PowerShell 控制台很快就开始打开。

也许您遇到的缓慢是 PowerShell 试图读取一个大的历史文件(该文件还不够大,不足以导致OutOfMemory)。


小智 2

您可以尝试创建一个快捷方式powershell.exe,右键单击它>属性,转到选项卡选项,单击“使用旧控制台”。我的屏幕阅读器(magic 和 Zoomtext)无法忍受秋季更新带来的“新”控制台(Powershell 非常慢),而遗留下来的一切又恢复正常了。