{"id":5541,"date":"2008-09-02T10:40:00","date_gmt":"2008-09-02T10:40:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/09\/02\/speeding-up-powershell-startup-updating-update-gac-ps1\/"},"modified":"2019-02-18T13:13:02","modified_gmt":"2019-02-18T20:13:02","slug":"speeding-up-powershell-startup-updating-update-gac-ps1","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/speeding-up-powershell-startup-updating-update-gac-ps1\/","title":{"rendered":"Speeding Up PowerShell Startup &#8211; Updating Update-Gac.ps1"},"content":{"rendered":"<p>Have I ever mentioned how much I love the community?&nbsp; Today Kirill Osenkov posted a comment on the blog pointing out that there was an API to get the .NET Framework install path:&nbsp; RuntimeEnvironment.GetRuntimeDirectory() .&nbsp; I didn&#8217; t know that and now I do.&nbsp; \ud83d\ude42<\/p>\n<p>I took the opportunity to rewrite Update-Gac.ps1 and in the process I decided to make it better.&nbsp; This illustrates the point I&#8217;ve made many times.&nbsp; With GUIs &#8211; you click buttons and that the end you what do you have?&nbsp; Maybe the system works, maybe it doesn&#8217;t and that is it.&nbsp; With scripting, you end up with a system that may or may not work (same as before) BUT you also produce an artifact (a script).&nbsp; When you share that script &#8211; the world changes.&nbsp; If your script is crap (or just doesn&#8217;t work) &#8211; people will look at it and tell you so and in the process they often tell you how to make it work or make it better.&nbsp; If it works &#8211; often people will show you better ways to accomplish the same task (as Kirill did today).&nbsp; <\/p>\n<p>Sometimes someone&#8217;s script will spark an inspiration &#8211; &#8220;I don&#8217;t need&nbsp;THAT but if it did THIS, that would be awesome!&#8221; and an incentive to create new scripts.<\/p>\n<p>One way or the other, the act of sharing a script improves your world.&nbsp; You get better.&nbsp; Others get better. The world gets better.<\/p>\n<p>&nbsp;So that is one thought.&nbsp; The other thought is that PowerShell is all about producing scripts <strong>OF THE RIGHT QUALITY<\/strong>.&nbsp; <\/p>\n<p>Does that mean PowerShell is all about producing high quality scripts?&nbsp; ABSOLUTELY NOT!<\/p>\n<p>High quality scripts are often very difficult and expensive to write.&nbsp; We absolutely want to support people that want to do this (we think about the guy who wants to run scripts that run the Federal Reserve Bank&#8217;s servers or the DataCenter at the CIA [I sure hope both of those environments write high quality scripts]).&nbsp; BUT we also want to support people that want quick and dirty scripts.&nbsp; <\/p>\n<p>The task at hand should determine the quality of the script.&nbsp; For my Update-Gac script, it was a quick and dirty script that I rarely use.&nbsp; Now that I&#8217;ve decided to share it, I get a little embarrassed by how raw it is so I invested a few minutes this morning to improve the script itself (thanks again to Kirill) and the user experience of the script.&nbsp; At that is the point, scripts are living things.&nbsp; That start out and then over time we craft them, buff them, take suggestions from other people, sometimes other people take our scripts and bring them to new levels.&nbsp; <\/p>\n<p>The efficient and effective creation and sharing of IT artifacts (scripts) is probably the biggest contribution PowerShell can make to the industry.&nbsp; Said another way, PowerShell doesn&#8217;t solve your IT problems.&nbsp; PowerShell helps the IT community to work together to solve it&#8217;s problems.&nbsp; This is precisely why we&#8217;ve added Graphical PowerShell (now called PowerShell Integrated Scripting Environment) to V2 &#8211; to make it easy to create and debug scripts.&nbsp; It is also why we added Modules &#8211; to make it easier and safer to share scripts.<\/p>\n<p>OK &#8211; enough with all that.&nbsp; Here is the new and improved Update-gac.ps1<\/p>\n<p>Set-Alias ngen (Join-Path ([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()) ngen.exe)<br \/>[AppDomain]::CurrentDomain.GetAssemblies() |<br \/>&nbsp;&nbsp;&nbsp; sort {Split-path $_.location -leaf} | <br \/>&nbsp;&nbsp;&nbsp; %{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Name = (Split-Path $_.location -leaf)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ([System.Runtime.InteropServices.RuntimeEnvironment]::FromGlobalAccessCache($_))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Already GACed: $Name&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host -ForegroundColor Yellow &#8220;NGENing&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : $Name&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ngen $_.location | %{&#8220;`t$_&#8221;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/p>\n<p><strong>&lt;Update 12\/4\/2008&gt; NOTE &#8211; this is a workaround for a V1 bug.&nbsp; This is fixed in V2.<\/strong>&nbsp;<\/p>\n<p>Cheers!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows Management Partner Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/08\/92\/01\/09\/update-Gac.ps1\">update-Gac.ps1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have I ever mentioned how much I love the community?&nbsp; Today Kirill Osenkov posted a comment on the blog pointing out that there was an API to get the .NET Framework install path:&nbsp; RuntimeEnvironment.GetRuntimeDirectory() .&nbsp; I didn&#8217; t know that and now I do.&nbsp; \ud83d\ude42 I took the opportunity to rewrite Update-Gac.ps1 and in the [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[333],"class_list":["post-5541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-update-gac"],"acf":[],"blog_post_summary":"<p>Have I ever mentioned how much I love the community?&nbsp; Today Kirill Osenkov posted a comment on the blog pointing out that there was an API to get the .NET Framework install path:&nbsp; RuntimeEnvironment.GetRuntimeDirectory() .&nbsp; I didn&#8217; t know that and now I do.&nbsp; \ud83d\ude42 I took the opportunity to rewrite Update-Gac.ps1 and in the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/5541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=5541"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/5541\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=5541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=5541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=5541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}