Using PowerShell to Install a DLL into the GAC

A couple of ways exist to install a DLL into the Global Assembly Cache (GAC). Using gacutil.exe is one, but this comes as part of a Visual Studio installation, and in a server environment, you may not have the luxury of installing Visual Studio, just to get the utility installed.

An alternative, however, is to use PowerShell to install the DLL into the GAC. The following code snippet should take care of your needs.

  • kiran gowda

    I am getting below errors .

    Exception calling “Load” with “1” argument(s): “Could not load file or assembly ‘System.EnterpriseServices,
    Version=1.0.0.0, Culture=neutral, PublicKeyToken=7f48c3199e5dff41’ or one of its dependencies. The system cannot fi
    the file specified.”
    At C:UsersAdministratorDesktoptest.ps1:1 char:1
    + [System.Reflection.Assembly]::Load(“System.EnterpriseServices, Version=1.0.0.0, …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FileNotFoundException

    New-Object : Cannot find type [System.EnterpriseServices.Internal.Publish]: verify that the assembly containing thi
    type is loaded.
    At C:UsersAdministratorDesktoptest.ps1:2 char:12
    + $publish = New-Object System.EnterpriseServices.Internal.Publish
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~
    + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

    You cannot call a method on a null-valued expression.
    At C:UsersAdministratorDesktoptest.ps1:3 char:1
    + $publish.GacInstall(“ctccs.dll”)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    • Exception shows you trying to load “System.EnterpriseServices, Version=1.0.0.0”, but the tutorial above calls for “System.EnterpriseServices, Version=4.0.0.0”. Can you confirm your code?