25

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

mike
  • 716

6 Answers6

12

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.

7

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

KetZoomer
  • 83
  • 12
4

There can be other reasons for what causes slow Powershell start, but something that always causes slowdown for me is Windows update.

Instead of running ngen on every assembly powershell happens to have loaded, what I want to do is run ngen update which optimises every globally installed assembly:

. (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.

2

What you could try is to create a shortcut to powershell.exe, right-click on it > properties, go to tab options, click on "use legacy console". My screenreader (magic and zoomtext) couldn't stand the 'new' console which came with the fall update (Powershell was veeeerrrrry slow) With legacy on everything works fine again.

KetZoomer
  • 83
  • 12
1

After analize powershell.exe with procmon (sysinternals tool) I could see that the process was trying to do something in catroot2 folder, so after rename it (you have to stop the CryptSvc service that blocks the folder), it was created automatically. So the conclusion is that folder got corrupted. (Sorry for my bad English)

Execute the next .bat

net stop CryptSvc /y
rename c:\windows\system32\catroot2 Catroot2.bak
net start CryptSvc
wolfy
  • 21
0

Found this solution when I googled having the same problem, but in 2022. Unfortunately this did not fix my issue.

Our systems have a group policy security requirement to "Turn on PowerShell Transcription". The policy requires that we specify "the Transcript output directory to point to a Central Log Server or another secure location". The server name changed and no one updated the policy. As soon as I updated the GPO with the new location, PowerShell opened instantly again.