Powershell slow starting on Windows 10

I have problem with slow starting of powershell prompt on Windows 10 ( Version 1703 - Creators Update ).

My hw specs ( quite fast machine ): Intel i5-7440HQ (Quad Core) / 32GB DDR4 RAM / 512 Samsung SSD hard drive.

I tried to bypass profile and execution policy but it does not change anything:

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

6,228067

My friends same laptop with Windows 10 without Creators Update runs powershell in less than 0,5 sec.

Also tried do some compilation with ngen.exe but it didn't help:

$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$_"}
}

Any idea how may I investigate this issue?

Greetings


This was happening to me also - though maybe not the best route, adding powershell.exe to the list of Windows Defender exclusions sped it up from 20 seconds to < 1 second.

Using legacy console, purging PSReadLine, and running ngen did not seem to help at all.


I had been experiencing the same issue for quite some time until PowerShell started failing on startup with the following error:

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)
-----------------------------------------------------------------------

This led me to the existing Github issue: https://github.com/Powershell/PSReadLine/issues/673

I tried deleting history file in ~\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ which was over 6 GB by then and after that PowerShell console started opening pretty quickly.

Maybe the slowness you experience is the PowerShell trying to read a big history file (which is not yet big enough to cause OutOfMemory).


Instead of ngening every assembly powershell loaded, what I tend to do whenever I update Windows 10 is run ngen update:

. (Join-Path ([Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) ngen.exe) update

After updating to May 2019 Win10, I see that improved the performance of

(measure-command { powershell.exe -command "1+1" }).TotalSeconds

from ~1.35s to ~0.55s.