Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell steps to fix slow startup

I have slow PowerShell console startup times (always more than 5 second wait) and was hoping for advice on troubleshooting steps to find out where the bottlenecks might be?

I have read that for running scripts, -NoProfile is important to prevent Modules etc loading, but how, in general, should we approach finding out what is slowing things down? I don't have many Modules installed and I know that since PowerShell 3.0, Modules are just referenced at startup and not fully loaded (a Module is only fully loaded when a function from a given Module is invoked) so I just can't understand why it takes 5+ seconds to start a bare console (my $profile also is empty).

Any advice on various steps that I can look at to debug the console startup process would be appreciated? Also, are there maybe some Microsoft or third-party tools that exist to debug the various steps in the console startup process to look for bottlenecks?

like image 772
YorSubs Avatar asked Dec 15 '19 05:12

YorSubs


People also ask

How do I repair Windows 10 PowerShell?

Type Turn Windows Features on or off in the Start Menu search bar and select the Best match. Locate the Windows PowerShell option and click its drop-down menu. Check all the Windows PowerShell boxes, press OK, and then close the Windows Features screen. Finally, restart your device to apply these changes.


2 Answers

When PowerShell starts to become slow at startup, an update of the .NET framework might be the cause. To speed up again, use ngen.exe on PowerShell's assemblies. It generate native images for an assembly and its dependencies and install them in the Native Images Cache.

Run this as Administrator

$env:PATH = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
[AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object {
    $path = $_.Location
    if ($path) { 
        $name = Split-Path $path -Leaf
        Write-Host -ForegroundColor Yellow "`r`nRunning ngen.exe on '$name'"
        ngen.exe install $path /nologo
    }
}

Hope that helps

like image 129
Theo Avatar answered Sep 22 '22 10:09

Theo


Step 1: Stop using PowerShell.

Now, seriously, something that needs ~13 seconds (YMMV) on an quad-core i7 cpu to launch off an ssd drive is an abomination of software architecture.

But yes, I hear you, "no viable alternative" etc...


... but if forced, bribed or blackmailed to still use it, check if your Windows has DNS cache service enabled.

For me, with DNS cache disabled and powershell executable firewalled, the built-in 5.1.19041.906 version starts quickly, but the new pwsh 7.1.4 would take around 13 seconds to get responsive to keyboard input under the same circumstances. It's so desperate to call home that it would just synchronously wait for some network timeout while blocking all user input, as if threads were a thing for the weak.

My resolution was to stick with the olden powershell 5.

like image 23
sunny moon Avatar answered Sep 20 '22 10:09

sunny moon