How to install a DLL to the GAC on Windows Server 2012 using only PowerShell (without having to install SDK or Visual Studio)

Prior to Windows Server 2012 I had been use to installing DLL files in the Windows Global Assembly Cache (GAC) by either opening the Windows/Assembly folder in Explorer and simply dragging and dropping the file, or by using GacUtil.exe

With Windows Server 2012 unfortunately it’s not quite so easy. Being able to simply open the GAC in Explorer and drag/drop is gone. Also GacUtil.exe is not present on the server by default.

PowerShell to the rescue. Here’s how to register a DLL called “MyDLL.dll” to the GAC (and also how to remove it.

For this example assume we have the “MyDLL.dll” file stored at c:\temp\MyDLL.dll

To add a DLL to the GAC

1. Run the SharePoint PowerShell console as Administrator

2. Enter the following PowerShell

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("c:\temp\MyDLL.dll")
iisreset

To remove a DLL from the GAC

1. Run the SharePoint PowerShell console as Administrator

2. Enter the following PowerShell

Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacRemove("c:\temp\MyDLL.dll")
iisreset

To view information for a DLL

Please refer to this article for how to retrieve more information for a assembly (DLL) using PowerShell including the assembly name, version, culture and public key token.

52 thoughts on “How to install a DLL to the GAC on Windows Server 2012 using only PowerShell (without having to install SDK or Visual Studio)

Add yours

      1. Thank you very much Sir, I am getting “Could not load file or assembly ‘System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified.”, I checked inside “C:\Windows\assembly” and found ‘System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ but not able to find Version 4.0.0.0 (win7 system with .NET 4.5), could you please suggest on this.

        Like

      2. Hi Selem,

        .Net4 assemblies don’t get installed in the c:\windows\assembly cache, rather it goes under %windir%\Microsoft.NET\assembly\.

        Ensure you are running as/with Administration rights.

        Like

  1. Hey Cameron,

    Thanks for that info regarding how to add the a custom DLL to the GAC. Could you please also let me know how to Set up a binding policy for the same DLL so all requests go to the correct version.

    Thanks and Regards,
    Jeby

    Like

  2. Thanks Cameron.
    One (noob?) question though, I’m running this on a Windows 2012R2 machine. Is the DLL supposed to show up in c:\windows\assembly after this as well?

    Cheers

    Like

  3. Hi,
    Thanks for this article it is really helpful,however when we are executing the above we are getting an exception on this line “iisreset”.Any idea why it is so.

    —V

    Like

  4. This is the error I am getting
    iisreset: the term ‘iisreset’ is not recognised as the name of a cmdlet, function, script file, or operable program

    Like

    1. iisreset is a command you can usually just type in at any command prompt to restart the Internet Information Server (Web Server Service) running on the server. Given it can’t find the program for you, I’m thinking that you may not be running the IIS service on this server. I put this command at the end of my script as the dll’s I’m installing are used by IIS to serve up web content, so I need to restart the web server after I install a new dll to the GAC. In your case you may be able to remove the iisreset. The dll should get installed into the GAC without this command. Hope that helps.

      Like

  5. We basically need to install some third party components there like winscp and a unzip tool.We ran the command without iisreset and it went off fine.But we are not able to locate where the dll’s are,we tried to see it here %windir%\Microsoft.NET\assembly\.Any help would be great .Thank you so much for helping me out with this .

    Regards
    Vandana

    Like

    1. It could be under one one the folders beneath %windir%\assembly (any .Net DLL prior to .Net4 will be under here). The exact folder location depends on the processor architecture it was compiled for and what the 3rd party developers decided to call it. Have a look under these folders here and if you can’t find it I suggest you contact the 3rd party providers. Good luck.

      Like

  6. Nice blog. I have tried to use your powershell to install a 32 .bit dll, but it didn’t work. What changes to your powershell script should I do? The dll is: Microsoft.TeamFoundation.Client.dll

    Like

      1. No, I didn’t receive any errors during the install, so, expecting it worked, I tried to run the application that relies on the dll, but keeps getting this message: Could not load file or assembly ‘Microsoft.TeamFoundation.VersionControl.Client, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies. The system cannot find the file specified.
        I did verify that the dll is not anywhere in the gac 32 & 64 bit. I’m trying to do this on 2012 server.

        Like

  7. What’s “System.EnterpriseServices.Internal.Publish” in “$publish = New-Object System.EnterpriseServices.Internal.Publish” line. I’m running each line separately and getting error on this one.

    Like

  8. I’m getting this error on the previous posted line “You cannot call a method on a null-valued expression”. My assembly full name is “DHAEPM2013, Version=1.0.0.0, Culture=neutral, PublicKeyToken=80fa4de326d6d432”

    Like

  9. Hey Cameron

    thanks for this useful post. Windows server is driving me crazy! So I build a .net assembly and am referencing it from a simple console application. The whole thing runs fine on my dev machine, but when I push it up to the server machine it crashes. I’m trying to install the referenced .dll into the GAC and run the script in the powershell. No errors -looks like it worked fine, but I cannot find the dll in any of the places I expected it… I tried looking in the following directories, but no joy:

    c:\Windows\Microsoft.NET\assembly\GAC_MSIL
    c:\Windows\Microsoft.NET\assembly\GAC_64
    c:\Windows\Microsoft.NET\assembly\GAC_32
    c:\Windows\assembly\GAC_MSIL
    c:\Windows\assembly\GAC_64
    c:\Windows\assembly\GAC_32

    I’m working with a 64 bit application, and using .NET v4. Is there something I’m missing? Other than looking for a folder with the name of my dll is there any other way I can check to see if the dll was correctly installed into the GAC?

    Cheers

    Like

    1. The error message you are getting and the line of PowerShell which the error occurs on may give some clue. Also does this dll have any dependencies (e.g. other dll’s or software that must be on the machine first?)

      Like

  10. Hey, thanks for your tuto ! But I’m trying to install Google.Apis.Auth.OAuth2 to the Gac as Google moved OAuth2 authentication this week for Google Analytics API. I can’t add it via Nuget as I’m using SSIS to pull GA API data. I ran the following command without getting any error message.

    Set-location “D:\TOOLS”
    [System.Reflection.Assembly]::Load(“System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)
    $publish = New-Object System.EnterpriseServices.Internal.Publish
    $publish.GacInstall(“D:\TOOLS\Google.Apis.Auth.dll”)

    …but there is not any Google.Apis.Auth.dll installed in C:\Windows\Microsoft.NET\assembly…Is it because the PublicToken is null ?

    Thanks a lot !

    Remi

    Like

  11. hi Cameron,
    on 2012 windows server, I don’t see system.web fodler into c:\windows\microsoft.net\assembly\gac_msil folder unlike folders I see for all others dll’s
    I used your approach to add system.web.dll assembly to gac_msil folder, completed all steps with out any issues till iisreset. But after these steps still System.Web fodler is not available at C:\Windows\Microsoft.NET\assembly\GAC_MSIL location. Any idea about this?

    Appreciate your help.

    Like

  12. Hi how can i execute dlls on multiple remove machine. when i used above code with invoke-command i am getting path related error.

    Like

  13. how to install a 200 dlls from the directory to the gac on windows server 2012 using only powershell. how to do that

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑