none
Powershell slow to start on clean install with empty profile present

    Question

  • I've recently run into an issue where with a clean installation of Windows, Powershell takes 3 seconds to start. Consider:

    new-item -type file -force $profile
    (Measure-Command {powershell -Command "% foo"}).TotalSeconds
    3.1043604

    However, on my other Windows 8.1 machines, I was seeing startup times for an empty profile at about .26 seconds (10x faster). I was beginning to suspect it was a hardware defect because this was a new laptop, identical to one I'd recently quit.

    I scrounged around the Internet for some time, finding lots of references to slow Powershell startup times when assemblies weren't cached, but they all seemed to suggest the issue was fixed in V2.

    Furthermore, the workarounds they referenced, using ngen, didn't work. The usage was no longer appropriate.

    Fortunately, I found this answer which worked great. After running it, startup times on the fresh Windows 8.1 laptop matched those for my other systems.

    I believe what I've discovered is a defect/regression in Windows 8.1. I have several systems that have had Windows 7 and Windows 8 and now have Windows 8.1, and I have not observed this slowness before, and I use Powershell many times per day.

    It was only after getting this laptop with Windows 8.1 pre-installed that I observed the problem. I spent hours uninstalling software, resetting to factory defaults, and installing with a clean install of Windows.

    Is there a reason Powershell on new installs of Windows 8.1 perform dramatically slower than in the past?

    Thursday, November 27, 2014 8:41 PM

Answers

  • Hi Jason R. Coombs,

    We performed a clean installation with Windows 8.1 Pro and did a lot of tests .

    It is really as you posted ,the power shell will run a little slowly in a clean installation of Windows 8.1. In our situation it will be about 5 seconds. We run the code in the link you posted and it will run faster about 0.7.

    After a deep research, we found the root cause. There are two tasks in the task scheduler ,they are related to the .net framework and the path is Task Scheduler Library\Microsoft\Windows\.NET Framework .

    When we perform a clean installation ,the tasks have not run. At this moment ,the power shell will run a little slowly.

    Here are the screenshots:

    After we manually ran the two tasks successfully ,the power shell will run faster about 0.7 seconds. It is corresponding to this issue.

    Best regards

    Wednesday, December 03, 2014 5:24 AM
    Moderator

All replies

  • Hi Jason R. Coombs,

    Does this issue occur only in the first time to start the power shell or every time?

    As we tested ,we don`t have this issue when we start the power shell in our Windows 8.1 Enterprise.

    According to your description and considering the code in the link will be useful ,this seems to relate to “ngen the assembiles in the background ”.

    Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly.

    Here are the link for reference:

    Update-Gac.ps1
    http://blogs.msdn.com/b/powershell/archive/2007/11/08/update-gac-ps1.aspx

    Speeding Up PowerShell Startup - Updating Update-Gac.ps1
    http://blogs.msdn.com/b/powershell/archive/2008/09/02/speeding-up-powershell-startup-updating-update-gac-ps1.aspx

    Apart from this ,here is another workground and it will disable the background operation.

    Save these code as  a powershell.exe.config file and save it in this path C:\Windows\System32\WindowsPowerShell\v1.0 .

    <configuration>
        <runtime>
            <generatePublisherEvidence enabled="false"/>
        </runtime>
     </configuration>

    Best regards

    Monday, December 01, 2014 11:23 AM
    Moderator
  • Hi MeipoXu

    > Does this issue occur only in the first time to start the power shell or every time?

    The issue occurs every time.

    > As we tested ,we don`t have this issue when we start the power shell in our Windows 8.1 Enterprise.

    I replicated this on a clean install after only creating a clean but empty profile. Did you repeat the commands as I wrote them above? What output did you get?

    Note also that I only experienced this issue on a clean install of Windows 8.1 (perhaps including updates). If your 8.1 Enterprise was updated from 8.0 or earlier, it almost certainly falls into the segment where I described that earlier systems or even Windows 8.1 upgraded from 8.0 didn't have this issue.

    > Here are the link for reference:

    Your links are unhelpful. Try to run those commands on Windows 8.1 and they will fail as the usage of ngen is no longer valid. However, the link I provided does the same thing and actually works.

    > Apart from this ,here is another workground

    Actually, that's not helpful either. As stated in my post, I've already solved the problem (by following the answer linked in my post).

    Most importantly, you failed to address with any confidence the one question in my post. I would be very much interested in hearing if anyone with a clean installation of Windows 8.1 doesn't have the issue I encountered.

    Monday, December 01, 2014 11:31 PM
  • Hi Jason R. Coombs,

    We performed a clean installation with Windows 8.1 Pro and did a lot of tests .

    It is really as you posted ,the power shell will run a little slowly in a clean installation of Windows 8.1. In our situation it will be about 5 seconds. We run the code in the link you posted and it will run faster about 0.7.

    After a deep research, we found the root cause. There are two tasks in the task scheduler ,they are related to the .net framework and the path is Task Scheduler Library\Microsoft\Windows\.NET Framework .

    When we perform a clean installation ,the tasks have not run. At this moment ,the power shell will run a little slowly.

    Here are the screenshots:

    After we manually ran the two tasks successfully ,the power shell will run faster about 0.7 seconds. It is corresponding to this issue.

    Best regards

    Wednesday, December 03, 2014 5:24 AM
    Moderator
  • Wow. Great answer. That certainly seems related. That's the task that runs ngen. So maybe that task would have been run at a later date if I'd just waited for whatever causes it to be run (maybe following certain updates or installing certain packages).

    If I look at my Scheduled Tasks now, I see those tasks were run this morning at about the time I turned on my laptop, so something is triggering them to run now.

    So it seems that easier than running the NGEN script manually, the easier thing I could have done is run this command:

    schtasks /Run /TN "\Microsoft\Windows\.NET Framework\.NET Framework NGEN v4.0.30319 64"

    And the same for the 32-bit version (omit " 64").

    Thanks for the investigation. I learned something new today.


    Thursday, December 04, 2014 3:32 PM