Using Powershell to Register a file in the Gac

James Turns picture James Turns · Mar 24, 2009 · Viewed 28.6k times · Source

Is there a simple way to in PowerShell (I imagine using gacutil.exe) to read from a text document a path\assembly and register it in the GAC? So for example a .txt file that looks like:

c:\test\myfile.dll
c:\myfile2.dll
d:\gac\gacthisfile.dll

The PowerShell script would read that into a stream and then run gacutil on each of those assemblies found? I guess it would be something like:

#read files into array?

foreach ($file in Get-ChildItem -Filter "*.dll" )
{ 
  Write-Host $file.Name
  C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\gacutil.exe /nologo /i $file.Name
}

Answer

Michał Romantowski picture Michał Romantowski · Apr 11, 2011

How about let the .Net worry about gacutil?

# load System.EnterpriseServices assembly
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") > $null

# create an instance of publish class
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish

# load and add to gac :)
get-content fileOfDlls.txt | ?{$_ -like "*.dll"} | Foreach-Object {$publish.GacInstall($_)}