Powershell command runs really slow on first run, then runs fine until powershell is closed

Daniel Sherwood 6 Reputation points
2022-06-23T09:32:53.22+00:00

OK so we deploy pipelines to a couple of servers, both are identical and get the same scripts but 1 shows the issue and the other does not. Both are Windows Server 2019 with the same specs

The issue simplified down is, when you open a Powershell window and run get-netfirewallrule for example, the first run takes 63 seconds to complete (See below), and any subsequent runs of that module within the same powershell window complete in under a second which is what I'd expect (See below).
If you close the PS window and open another another, the same thing happens (60seconds then 1s). When we are deploying pipelines multiple ps scritps are called and certain commands see this issue meaning app deployments are taking 10-20 minutes longer than they should. Anyone seen this before? Gary Google is currently failing me :-) and the DEV team keep nudging me for a resolution.

thanks

PS C:\Windows\system32> Measure-Command {(get-netfirewallrule).count}
TotalMinutes : 1.06570219833333
TotalSeconds : 63.9421319
TotalMilliseconds : 63942.1319

PS C:\Windows\system32> Measure-Command {(get-netfirewallrule).count}
TotalMinutes : 0.010560705
TotalSeconds : 0.6336423
TotalMilliseconds : 633.6423

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,674 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,625 questions
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 35,416 Reputation points
    2022-06-23T12:40:55.957+00:00

    If this were my problem, I would first do a basic health check on cpu/memory/disk. Then a deeper dive into the disk performance of the C drive. Verify that it's responding ok and that I/O requests are not being queued.

    Then I would run Process Monitor to see what I/O Powershell is doing.

    https://learn.microsoft.com/en-us/sysinternals/downloads/procmon

    In the Filter menu option, set an "include process name is powershell.exe". Then open a PS prompt and run the command. Stop the trace and from the Tools menu review the various Summary reports to try to figure out what is taking the most time.

    You might have to run that same analysis on a "good" server to see if you can find a difference somewhere.

    0 comments No comments

  2. Limitless Technology 39,796 Reputation points
    2022-06-27T07:45:21.867+00:00

    Hi there,

    This is due to getting the .NET underpinnings loaded. Of course this was never a requirement for cmd.exe so it does make PS look really slow on startup. Subsequent loads, as you noticed, do not suffer from this.

    The slowness of the PowerShell Pipeline may also be a script issue, and simply by replacing Foreach-Object with the new Foreach-ObjectFast, and Where-Object with the new Where-ObjectFast might significantly increase the speed.

    -------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.