Update-Gac.ps1

Update-Gac.ps1

  • Comments 39

Below is the content of the Update-GAC.ps1 script that I run whenever I install a new version of PowerShell. Our installation is supposed to ngen the assemblies in the background. If that works, it doesn't work fast enough for me. Also I've seen lots of examples where people run this script long after their install and things get a TON faster so …. The install team is looking into the issue but until then – here is the script I use:

Set-Alias ngen @(
dir (join-path ${env:\windir} "Microsoft.NET\Framework") ngen.exe -recurse |
sort -descending lastwritetime
)[0].fullName
[appdomain]::currentdomain.getassemblies() | %{ngen $_.location}

<Update 12/4/008> NOTE - there is a updated (and better) version of this script HERE.

Cheers!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • PingBack from http://msdnrss.thecoderblogs.com/2007/11/08/update-gacps1/

  • I noticed that when this ran that it was unable to compile assemblies that were loaded by PSCX (PowerShell Community Extensions).  How would you modify this script to handle assemblies that belong to other snap-ins?

  • Technically you're not updating the GAC. You're updating the Native Image Cache, or NIC. These are actually both foldered directly under %WINDIR%\assembly.

  • This script has an oversight and does not locate ngen correctly. It does not work properly since I have a _folder_ called "ngen.exe" located here: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\Bind Logs\ngen.exe\

  • Adding a filter for item length works better since you filter only files that way, and don't accidentally include bind log directories that are named ngen.exe:

    set-alias ngen @(dir -recurse (join-path ${env:\windir} "Microsoft.NET\Framework\") ngen.exe | where {$_.length -gt 0} | sort -descending lastwritetime)[0].fullname

    [appdomain]::currentdomain.getassemblies() | %{ngen $_.location}

  • > since I have a _folder_ called "ngen.exe"

    I'm dying to know:  Why do you have a folder with an EXE name?

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • On Vista, you need to start Windows PowerShell with the "Run as administrator" option to run this successfully.

    When it fails, the output contains a bunch of "access denied" messages.

  • This is a great bit of script.  I've shared this out with several people on my team, and it's resolved all of the "PowerShell is slow to start" comments.

    Can you say anything about whether this will still be useful/needed on PowerShell v2?  I understand wanting the installation to be responsive, but I'd rather wait an extra minute or two during the installation if it means that PowerShell starts more quickly the first time.

  • Jeffrey Snover posted (back in November of 2007) a script that solves the "PowerShell starts slowly"

  • > Can you say anything about whether this will still be useful/needed on PowerShell v2?

    We will have the problem fixed in V2.

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • I talked about this before but a number of people have missed it so here it is under a better title.

  • WOW - no real numbers but significant reduction in startup time

  • Annoyed with how long PowerShell takes to startup?&#160; Run this script… http://blogs.msdn.com/powershell

  • OMG!

    Slow startup times were actually my (only) ONE major complaint about PowerShell, though I assumed there was nothing you could do about it so I hadn't complained about it.

    WAY COOL! THANKS!

  • Great Script,

     Be aware, you need administrator access on the workstation to complete the execution.

Page 1 of 3 (39 items) 123