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.

Advertisements

About Cameron Dwyer

Architect and developer at OnePlace Solutions. Passionate about delivering compelling solutions on the Office 365/SharePoint platform. Addicted to coffee.

Posted on May 22, 2013, in PowerShell and tagged , , , , , , , . Bookmark the permalink. 49 Comments.

  1. Thanks a lot it helped me a lot 🙂

    Like

  2. I applied it but now I need to get the public token it was register with. Can you help me with that?

    Like

  3. Jeby Mathew John

    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

  4. Thanks, it saved me alot of time.

    Like

  5. Muhammad Khalid Latif

    Thanks cameron, doing great job

    Like

  6. 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

  7. 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

  8. 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

    • 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

  9. 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

    • 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

  10. Thank you for your fast response.Appreciate it

    Like

  11. 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

    • Are you getting an error message when you try to install the dll? 32/64 bit should both work (they get installed.in different locations but scrupt should be the same.

      Like

      • 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

  12. Thank you so much – I was having trouble with this one thing and you’ve fixed it!!!

    Like

  13. Thank you !!! … Nice Article…

    Like

  14. Nice Article , thanks very much

    Like

  15. 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

  16. 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

  17. Reblogged this on Decoupled Logic and commented:
    Easy way to install DLL to GAC without SDK or VS.

    Like

  18. 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

  19. Thanks for this. Saved me a ton of time and a lot of downloads 🙂

    Like

  20. Thank you and thank google for leading me here. I was getting desperate 🙂

    Like

  21. Hassan IBN ZBIBA

    Thx

    Like

  22. Thanks.. It works for me 🙂

    Like

  23. There is no share point powershell and i have only windows powershell and i tried to register but unable to register Microsoft.Practices.ObjectBuilder2.dll

    Like

    • 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

  24. Thank you Sir, all looks fine now.

    Like

  25. 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

  26. Thanks Cameron,

    This helped me.

    Like

  27. 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

  28. wow I shoud have found this earlier:)

    Liked by 1 person

  29. 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

  1. Pingback: How to find the Version, Culture and PublicKeyToken of an Assemby (DLL) using PowerShell | Cameron Dwyer | SharePoint, Outlook, OnePlaceMail

  2. Pingback: [SSIS] Enregistrer une DLL pour un Script Task | La BI et les outils Microsoft

  3. Pingback: Adding a Custom DLL to a Windows 2012 Server | SperryH

  4. Pingback: How to install a DLL to the GAC on Windows Server 2012 using only PowerShell (without having to install SDK or Visual Studio) | Brian By Experience

  5. Pingback: Agregar /Eliminar DLL del GAC sin GacUtil | Desarrollo de Coquito

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: