/build/static/layout/Breadcrumb_cap_w.png

HOW TO: Uninstall Program by Name Only

Okay guys, here's something I've been working on. The idea is to pass the below script the name of a program, the script finds the uninstall string and removes it. My idea was this could be used on toolbars, stuff like itunes, etc. Your not gonna want to create a script or distribution for every version (i've done this in the past). Here you would just give it the name of the program you want gone, and if it knows how to uninstall it, it will.

Curerntly supported uninstall strings:

  • Msiexec commands - These are pretty easy
  • Stuff that ends in uninstall.exe - These are usually /S. 
  • Stuff that ends in helper.exe - These are usually /S



Here's the uninstall script. I wrote it in powershell. Save it as "uninstallScript.ps1" :
###########################################
######## Written by DC  2012-02-13#########
###########################################
<#

.SYNOPSIS

Uninstalls software by only passing the Software Title.
Should work with all msiexec string uninstallers. 
For uninstall commands that end in uninstall.exe or helper.exe a "/S" is used as a switch.

.PARAMETER DisplayName

The complete or partial name of the software being uninstalled. Must appear as shown in add / remove programs (case insenstive).


.EXAMPLE

Uninstall-Program Java

Will search the registry and uninstall all instances of Java from a machine.

#>

[cmdletBinding()]
Param
(
[String]$DisplayName = $(throw "DisplayName is Required")
)

Set-Variable -Name ThirtyMachine -Value "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
Set-Variable -Name SixtyMachine -Value "HKLM:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
Set-Variable -Name ThirtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
Set-Variable -Name SixtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant

$regs = $ThirtyMachine,$SixtyMachine,$ThirtyUser,$SixtyUser

foreach ($reg in $regs)
{
if(Test-Path $reg)
{
$SubKeys = Get-ItemProperty "$reg\*"
}
else
{
$SubKeys = $null
}
foreach($key in $SubKeys)
{
if($key.DisplayName -match "$DisplayName")
{

Write-Host "Found Software " $key.DisplayName
if($key.UninstallString -match "^msiexec")
{
$startGUID = $key.UninstallString.IndexOf("{") + 1
$endGuid = $key.UninstallString.IndexOf("}") - $startGUID
$stringer = $key.UninstallString.Substring($startGUID,$endGuid)
Write-Host "Uninstaller Known, now uninstalling"
&msiexec `/qn `/x `{$stringer`}

}
if($key.UninstallString.Replace('"',"") -match 'uninstall.exe\Z' -or $key.UninstallString.replace('"',"") -match 'helper.exe\Z' )
{
$stringer = $key.UninstallString.Replace('"',"")
if(Test-Path $stringer )
{
Write-Host "Possible Uninstaller found. Trying"  $key.UninstallString "/S"
&$stringer /S
}
}
}
}
}


From there you can create a new shell script and upload the ps1 as a dependency. Here's what i have for script text. This example uninstalls all versions of Java. You could try your software there. 
powershell.exe -nologo -executionpolicy bypass -WindowStyle hidden -noprofile -file "uninstallScript.ps1" "Java"
Remember to change the script name (at the bottom) from script.sh to script.bat.

Hopefully this helps somebody.


Comments

  • Excellent resource, thanks! I've been reviewing ways of dealing with a lot of the dated systems on my network that have up to 20 versions of Java and/or Adobe Reader installed and this looks much better than other approaches I've seen. I'll need to test (of course), but am hoping I'll be able to use this in a managed install driven by a smart label (ex: if a system doesn't have the latest version of Java, apply the smart label which will in turn trigger the MI to first search for & uninstall all old versions, then deploy the current version). Not sure if this will be feasible or not since I have done neither MIs nor app deploys triggered by smart labels, but it sounds like it should be possible based on what I've read and others have told me. Fingers crossed and thanks again! ;)

    John - jverbosk 9 years ago
    • Glad you like it, that's exactly what i was going for. If you know any other common uninstall commands, please share! - dchristian 9 years ago
      • Hi dChristian

        I tried to use this SCript in Windows 7. it worked fine. But When I tried to use it in Windows XP it failed. Any idea? - V_siddhaarth 9 years ago
  • WOW that is AWESOME. Great work on this. I will definitely be using this. - ohiosoundguy 9 years ago
  • Nice Job. - dan@kace.com 9 years ago
  • I have had some success with uninstalling apps via WMI. I.E.

    $app = gwmi win32_product | where-object { $_.name -match "" }
    $app.uninstall() - muebel 9 years ago
  • How to use it in Windows XP SP2? - V_siddhaarth 9 years ago
  • You need SP3 to use Powershell. - dugullett 9 years ago
  • I'm new to scripts. which is the script.bat file and witch is the ps1 file. ? - Kdebiasse 8 years ago
    • The ps1 is at the top starting at #####################, and ending with the last }. You will need to take all of that and save it as a .ps1.

      The reason you change to .bat in the lower section is because the script is actually calling powershell.exe. If you let the "script file name" at .sh it would try and execute a shell script. Changing that line to .bat just makes it available to run on Windows.

      Does that make sense? - dugullett 8 years ago
  • thanks - Kdebiasse 8 years ago
  • Is there anyway to remove the directories from the c drive. I wnet back to look on the c drive and the java folder is still there. - Kdebiasse 8 years ago
  • Thanks, this works well. Is there a way for me to run this and then when it is finished running i run the new msi that has the new version? - fiestasynergy 8 years ago
  • Is there a way to remove more than one program. IE adobe and java. Plus Adobe does not remove flash or wave. - Kdebiasse 8 years ago
  • Also It does not work with coupon.com program. - Kdebiasse 8 years ago
  • This script works great when uninstalling single applications. I am new to powershell so I created a vbscript to execute the powershell commands. However, I have 5 applications to uninstall and my vbscript attempts to uninstall all the applications at the same time. VBScript it working correctly but does not wait for the uninstall command to finish.

    How can I use this script to uninstall all 5 apps which start CSC ?

    Any ideas would be very useful. - spadge007 7 years ago
    • I haven't tested, but this should work.

      At this line add START /WAIT:

      &msiexec `/qn `/x `{$stringer`}

      change to

      &START /WAIT msiexec `/qn `/x `{$stringer`}

      This should allow each uninstall to finish before starting the next. - dugullett 7 years ago
      • Thanks for the speedy response but that change doesn't appear to work. I ran the following command within powershell and not vbscript

        .\uninstallScript.ps1 "CSC"

        I get the following error

        .\UninstallScript.ps1 "CSC"
        Found Software CSC License Service
        Uninstaller Known, now uninstalling
        Start-Process : A positional parameter cannot be found that accepts argument '/
        qn'.
        At C:\Users\test\Desktop\PSTest\UninstallScript.ps1:60 char:2
        + & <<<< START /WAIT msiexec `/qn `/x `{$stringer`}
        + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterB
        indingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
        .Commands.StartProcessCommand

        I amended line 60 as you suggested to:
        &START /WAIT msiexec `/qn `/x `{$stringer`} - spadge007 7 years ago
      • Not sure if anyone resolved this, but I am having the same issue.

        Uninstall.ps1:60 char:1
        + &msiexec `/qn `/x `{$stringer`}
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : InvalidOperation: ({{0}}}:String) [], RuntimeExc
        eption
        + FullyQualifiedErrorId : FormatError - DaBXRoMeO 4 years ago
  • I cannot get this to work via K1000. If i run it from a batch, fully qualifying the path to the ps1 file, it does work.

    Running as System, if i do or do not qualify the path (even added a step before the powershell step copying the file to a common folder) in k1000 it does not work.

    Running as my ID, I don't have to qualify the path to the ps1 file but msiexec throws an error in the event log stating that i must be an administrator to run it. My id IS in the local Admins group so I'm guessing this has to do w/ it needing elevation. - adaml 7 years ago
  • Figured it out. Had to run powershell from the sysnative context.
    "%SystemRoot%\sysnative\WindowsPowerShell\v1.0\powershell.exe"
    When is the k1000 agent going to be a 64bit application? Criminy! - adaml 7 years ago
  • Set-Variable -Name ThirtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
    Set-Variable -Name SixtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant

    Is seems to me this is incorrect. It should be:

    Set-Variable -Name ThirtyUser -Value "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant
    Set-Variable -Name SixtyUser -Value "HKCU:\SOFTWARE\WOW6432NODE\Microsoft\Windows\CurrentVersion\Uninstall" -Option Constant - Eric-Jan H te A 6 years ago
  • Hi

    how do you add a log at the statement? I will like to see a log file created i.e. c:\temp\softwarename_uninstall.log - artifect 5 years ago
This post is locked
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ