Friday, September 16, 2011

Remove the dll from GAC without using GAC util

Using the following name space :System.EnterpriseServices.Internal, you can uninstall or remove assembly from GAC without using GacUril in your production environment.

I have seen on web, that people are getting hard time to use Publish.GacRemove method, because they don't know how to give the path name.
Here are two step to uninstall the dll from GAC without using GacUtil.

STEP 1:

Copy the existing dll into a temp folder and then use that path to remove/uninstall. Here is the Powershell code to copy the dll from gac to directory:
dir C:\Windows\Assembly -Recurse -Filter "YourDLLName*.dll" foreach { copy $_.FullName C:\Temp\assemblies }

STEP 2:
Here is the powershell code to remove the dll from gac using path in step1

# load System.EnterpriseServices assembly
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices, Version=2.0.0.0")

# create an instance of publish class
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish

#UnInstall UserTracking.DAL.dll in gac
$AssemblyFullName = "C:\Temp\Assembly\YourDLLName*.dll"
$publish.GacRemove($AssemblyFullName)

dll name in $AssemblyFullName = "C:\Temp\Assembly\YourDLLName*.dll", is copied from GAC to C:\Temp\Assembly folder in step1.

I hope this article will help to someone, please plant a tree in your life span if you get any help from this as a donation to www.code4green.com

4 comments:

  1. step 1 is not working for me

    Get-ChildItem : A positional parameter cannot be found that accepts argument 'foreach'.
    At C:\Search and Replace\assemblychecker.ps1:1 char:1
    + dir C:\Windows\Assembly -Recurse -Filter "YourDLLName*.dll" foreach { copy $_.Fu ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

    ReplyDelete
  2. [Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices, Version=2.0.0.0");
    [System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish
    $publish.GacRemove($file.FullName);

    ReplyDelete
  3. I tried following the above steps, however after running the command $publish.GacRemove($AssemblyFullName), I still see the DLL present in the temp location as well in the actual GAC assembly folder from where the dll was copied. Please suggest if i'm doing anything wrong.

    ReplyDelete