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.
Thanks a lot it helped me a lot 🙂
LikeLike
I applied it but now I need to get the public token it was register with. Can you help me with that?
LikeLike
Hi Sarah, I’ve just created this very small follow up post to show you how to retrieve more information for a assembly (DLL) using PowerShell including the assembly name, version, culture and public key token. I hope this answers your question.
https://camerondwyer.wordpress.com/2013/11/01/how-to-find-the-version-culture-and-publickeytoken-of-an-assemby-dll-using-powershell/
LikeLike
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.
LikeLike
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.
LikeLike
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
LikeLike
Hi Jeby, the following links (from Microsoft) explain the DLL binding process and the different ways you can redirect. Essentially you can redirect at the Machine level, Application level, or Assembly level (if you are the vendor of the assembly)
http://msdn.microsoft.com/en-us/library/7wd6ex19(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/yx7xezcf.aspx
http://msdn.microsoft.com/en-us/library/8f6988ab.aspx
LikeLike
Thanks, it saved me alot of time.
LikeLike
Thanks cameron, doing great job
LikeLike
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
LikeLike
Hi Arjan,
If the assembly is a .Net4 assembly it no longer gets installed in the c:\windows\assembly cache, rather it goes under %windir%\Microsoft.NET\assembly\
Take a look at this link for more details:
http://stackoverflow.com/questions/2660355/net-4-0-has-a-new-gac-why
This may explain what you are seeing.
LikeLike
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
LikeLike
Hard to answer without any detail of the error you are getting. If I had to guess I’d go for a security problem. Are you logged in as an administrator who can perform a restart of IIS? Also check the User Access Control settings (UAC) as this may be preventing you from performing the restart. http://windowsitpro.com/windows-server-2012-r2/changing-uac-behavior-windows-server-2012-r2-and-windows-81
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
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.
LikeLike
Thank you for your fast response.Appreciate it
LikeLike
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
LikeLike
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.
LikeLike
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.
LikeLike
Thank you so much – I was having trouble with this one thing and you’ve fixed it!!!
LikeLike
Thank you !!! … Nice Article…
LikeLike
Nice Article , thanks very much
LikeLike
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.
LikeLike
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”
LikeLike
Reblogged this on Decoupled Logic and commented:
Easy way to install DLL to GAC without SDK or VS.
LikeLike
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
LikeLike
Ensure your DLL is signed with a strong name key before attempting to add it to the GAC. This article contains a few ways to see if the DLL is registered:
http://stackoverflow.com/questions/689072/how-to-check-if-a-dll-is-registered
LikeLike
Thanks for this. Saved me a ton of time and a lot of downloads 🙂
LikeLike
Thank you and thank google for leading me here. I was getting desperate 🙂
LikeLike
Glad to be of assistance!
LikeLike
Thx
LikeLike
Thanks.. It works for me 🙂
LikeLike
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
LikeLike
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?)
LikeLike
Thank you Sir, all looks fine now.
LikeLike
Did the issue end up being permissions related?
LikeLike
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
LikeLike
Thanks Cameron,
This helped me.
LikeLike
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.
LikeLike
wow I shoud have found this earlier:)
LikeLiked by 1 person
Hi how can i execute dlls on multiple remove machine. when i used above code with invoke-command i am getting path related error.
LikeLike
https://www.microsoft.com/en-za/download/details.aspx?id=35585
LikeLike
how to install a 200 dlls from the directory to the gac on windows server 2012 using only powershell. how to do that
LikeLike