Welcome to MSDN Blogs Sign in | Join | Help

Update-Gac.ps1

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

Published Thursday, November 08, 2007 5:39 PM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings &raquo; Update-Gac.ps1

Thursday, November 08, 2007 12:57 PM by MSDN Blog Postings » Update-Gac.ps1

# re: Update-Gac.ps1

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?

Thursday, November 08, 2007 3:23 PM by Chris Lieb

# re: Update-Gac.ps1

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.

Friday, November 09, 2007 11:35 AM by Heath Stewart

# re: Update-Gac.ps1

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\

Friday, November 23, 2007 9:47 AM by Anonymous

# re: Update-Gac.ps1

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}

Friday, November 23, 2007 10:38 AM by Anonymous

# re: Update-Gac.ps1

> 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

Sunday, November 25, 2007 11:27 AM by PowerShellTeam

# re: Update-Gac.ps1

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.

Friday, June 13, 2008 8:50 PM by June Blender

# re: Update-Gac.ps1

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.

Thursday, July 10, 2008 7:56 PM by JaredReisinger

# PowerShell speed-up script

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

Thursday, July 10, 2008 8:22 PM by Abstract Semantics

# re: Update-Gac.ps1

> 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

Thursday, July 10, 2008 10:22 PM by PowerShellTeam

# Speeding Up PowerShell Startup

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

Friday, July 11, 2008 8:16 AM by Windows PowerShell

# re: Update-Gac.ps1

WOW - no real numbers but significant reduction in startup time

Friday, July 11, 2008 12:14 PM by Bruce

# Fix PowerShell startup times with a PowerShell script

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

Friday, July 11, 2008 3:46 PM by Rod Trent at myITforum.com

# re: Update-Gac.ps1

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!

Friday, July 11, 2008 11:55 PM by Mike Schinkel

# re: Update-Gac.ps1

Great Script,

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

Monday, July 14, 2008 10:28 AM by Shaun Cassells

# re: Update-Gac.ps1

Don't forget that on x64 systems, you need to use "Microsoft.NET\Framework64" instead.

Monday, July 14, 2008 8:14 PM by DM

# re: Update-Gac.ps1

Consider passing "/nologo" to ngen.exe, to reduce noise.

Friday, July 18, 2008 1:30 PM by Jay Bazuzi

# re: Update-Gac.ps1

Great info (as always)!

As I'm not a .NET-guy, where can I find some backgroud info on GAC, ...

Monday, July 21, 2008 2:02 AM by robby

# Speeding Up PowerShell Startup

Jeffrey Snover [MSFT]: In V1, we had a problem which caused our assemblies to not get ngen&#39;ed during

Wednesday, July 23, 2008 3:33 AM by HyperVoria

# re: Update-Gac.ps1

I made a couple of tweeks to add detection of x86 and AMD64:

Set-Alias ngen @(

$ngen_path = Join-Path ${env:\windir} "Microsoft.NET\Framework"

if(${env:PROCESSOR_ARCHITECTURE} -eq "AMD64"){

$ngen_path = Join-Path ${env:\windir} "Microsoft.NET\Framework64"

}

dir $ngen_path ngen.exe -recurse | where {$_.length -gt 0} | sort -descending lastwritetime

)[0].fullName

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

Wednesday, July 23, 2008 7:10 PM by Justin

# re: Update-Gac.ps1

Can you use this to speed up Exchange Management Shell as well? As this always take a long time to load up!

Wednesday, July 23, 2008 10:26 PM by Fred

# re: Update-Gac.ps1

Yes it does speed up the exchange managment shell.  it is still a little slow, but a major improvement.

Thursday, July 24, 2008 12:09 PM by Justin

# re: Update-Gac.ps1

FANTASTIC!  Thank you!

I tested on XPsp2 and Vista Ultimate sp1 - both with great success.  The above comments helped too.

Thursday, July 24, 2008 5:22 PM by EDF

# More on Update-GAC (speeding up PowerShell startup)

Jeffrey Snover of Microsoft, PowerShell dude extraordinaire, recently reminded us of a way to Speed Up

Friday, July 25, 2008 3:10 PM by Michael's meanderings...

# PowerShell Startup

If you think your PowerShell startup is slow, or even if you think it is OK, follow the instructions

Saturday, July 26, 2008 8:24 PM by Richard Siddaway's Blog

# re: Update-Gac.ps1

Am I correct in assuming that this script, in order to retain its benefits, would need to be re-run after any update or patch to Exchange or the .NET Framework?

Friday, August 01, 2008 4:34 PM by Glen Martin

# re: Update-Gac.ps1

Saturday, August 09, 2008 1:27 AM by japanese

# re: Update-Gac.ps1

Error for System.Data.dll on x64 PowerShell.

For some reason ngen always fails to comile a native image of System.Data.dll on PowerShell x64. Here's the relevant message.

Microsoft (R) CLR Native Image Generator - Version 2.0.50727.3053

Copyright (c) Microsoft Corporation.  All rights reserved.

Installing assembly C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll

Failed to find dependencies of image C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll be

cause this image or one of its dependencies is not a valid Win32 application.

   Compiling assembly C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll ...

Error compiling C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll: An attempt was made to

load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

Uninstalling assembly C:\Windows\assembly\GAC_64\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll because of an err

or during compilation.

Wednesday, August 13, 2008 11:44 AM by Brian Reiter

# re: Update-Gac.ps1

There's a pretty easy way to get the framework path. I noticed some commenters trying to make it find the right version of ngen or what not...

$FrameworkPath = [System.IO.Path]::GetDirectoryName([Object].Assembly.Location)

$NgenPath = [System.IO.Path]::Combine($FrameworkPath, "ngen.exe")

Friday, August 22, 2008 3:48 PM by Josh Einstein

# re: Update-Gac.ps1

@Brian - Making Microsoft.NET\Framework into Microsoft.NET\Framework64 as DM said.

Tuesday, August 26, 2008 9:17 AM by Yuvi

# re: Update-Gac.ps1

One can also determine the .NET framework install path using RuntimeEnvironment.GetRuntimeDirectory()

http://blogs.msdn.com/kirillosenkov/archive/2008/05/07/how-to-determine-the-net-installation-directory-and-clr-version.aspx

Tuesday, September 02, 2008 4:03 AM by Kirill Osenkov

# re: Update-Gac.ps1

I tried this Script and it does not do any thing to speed the start-up of poweshell console for Exchange 2007 installed on a Windows Server 2008!!! Any idea on how to fix this??

Thanks.

Sunday, September 28, 2008 4:15 AM by Feras Mustafa

# Speed up PowerShell

Jeffrey Snover just dropped this little gem into the " PowerShell: Creating Manageable Web Services "

Thursday, October 30, 2008 1:06 PM by Howard van Rooijen's Blog

# Speed up Powershell V2?

I ran the script in V2, still takes on the order of 15-20 seconds to get to PS when issuing powershell.exe from cmd.  I'm worried about the scripting speed.  I need to poll 300 machines repeatedly for very specific info.  It's starting to sound like I need to use a binary or maybe wscript

Friday, October 31, 2008 6:18 PM by Josh Smith

# re: Update-Gac.ps1

@Josh

That doesn't sound right - we should be able to meet your needs.  Can you do the following and post it:

1..10 | %{Measure-Command {powershell exit}}|ft totalseconds

gwmi win32_processor |fl name,NumberOfCores,MaxClockSpeed,L2CacheSize

gwmi Win32_PhysicalMemory |ft tag,speed,{$_.capacity/1mb} -auto

Thanks!

jps

Friday, October 31, 2008 7:03 PM by PowerShellTeam

# re: Update-Gac.ps1

Even after running this script my powershell takes > 20 seconds to start (either from shortcut or by typing powershell from cmd).  Starting it from within powershell itself is fast.

Here is the output from the commands Josh suggested:

PS C:\Documents and Settings\jdease>  1..10 | %{Measure-Command {powershell exit}}|ft totalseconds

                                                                                                          TotalSeconds

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

0.6983097

0.6771509

0.7174827

0.6861359

0.6862929

0.7018789

0.6871349

0.6774461

0.6860443

0.6863205

PS C:\Documents and Settings\jdease> gwmi win32_processor |fl name,NumberOfCores,MaxClockSpeed,L2CacheSize

name          : Intel(R) Core(TM)2 CPU          6400  @ 2.13GHz

NumberOfCores : 2

MaxClockSpeed : 2128

L2CacheSize   : 2048

PS C:\Documents and Settings\jdease> gwmi Win32_PhysicalMemory |ft tag,speed,{$_.capacity/1mb} -auto

tag               speed $_.capacity/1mb

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

Physical Memory 0   667            1024

Physical Memory 2   667            1024

Thursday, November 13, 2008 3:07 PM by Jeff

# re: Update-Gac.ps1

Mine went to .5 secs

don't know what it was before, but this put some pep in it's step (both ways)

Wednesday, April 29, 2009 6:10 PM by radhat

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker