Using Powershell to Register a file in the Gac Using Powershell to Register a file in the Gac powershell powershell

Using Powershell to Register a file in the Gac


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($_)}


If you sort out your text file such that the each dll is on a separate line, you could use the Get-Content command and pipe each to a filter that did your command:

filter gac-item { C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\gacutil.exe /nologo /i $_}get-content fileOfDlls.txt | ?{$_ -like "*.dll"} | gac-item


I would suggest calling the function to add an assembly to the GAC something following PowerShell guidelines like Add-GacItem. Also the location of gacutil.exe varies based on your system. If you have VS 2008 installed, it should be at the location shown below.

function Add-GacItem([string]$path) {  Begin {    $gacutil="$env:ProgramFiles\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe"    function AddGacItemImpl([string]$path) {      "& $gacutil /nologo /i $path"    }  }  Process {    if ($_) { AddGacItemImpl $_ }  }  End {    if ($path) { AddGacItemImpl $path }  }}Get-Content .\dlls.txt | Split-String | Add-GacItem

Note that the Split-String cmdlet comes from Pscx. The function isn't super robust (no wildcard support doesn't check for weird types like DateTime) but at least it can handle regular invocation and pipeline invocation.