locked
Using Registry UninstallString to remove software RRS feed

  • Question

  • I have some code that will search the registry for specified software, and return the UninstallString. I am trying to make this into a script that will uninstall various software. For example, this:

    Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall,
    HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
        Where-Object {$._DisplayName -match "java" |
        Select-Object -Property DisplayName, UninstallString

    when run will return this:

    DisplayName                             UninstallString                                                   
    -----------                             ---------------                                                   
    Java 7 Update 40 (64-bit) 		MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F86417040FF} 
                 
    Java 7 Update 40 			MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F83217040FF}  

    I'm still not sure how to call the uninstallstring though. This works with Get-WmiObject, but is incredibly slow:

    $app = Get-WmiObject -Class Win32_Product | Where-Object { 
    $_.Name -match "Java"    }
    $app.Uninstall()
    So how can you call the uninstallstring to execute and remove the program when using the Get-ChildItem method?

    • Edited by commdudeaf Monday, September 23, 2013 6:06 PM
    Monday, September 23, 2013 6:02 PM

Answers

  • Okay, this seems to work for me (testing locally only though):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            & cmd /c $uninst /quiet /norestart
        }
    
    }


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)

    • Marked as answer by commdudeaf Tuesday, September 24, 2013 12:35 PM
    Monday, September 23, 2013 7:56 PM

All replies

  • Hi,

    Give this a shot (you'll need to adjust this if you want it to run fully silently):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            $uninst = $uninst.Replace('{',' ').Replace('}',' ')
            Invoke-Expression $uninst
        }
    
    }

    EDIT: I should mention - Invoke-Expression can be very, very dangerous. Be careful.


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)


    Monday, September 23, 2013 6:25 PM
  • Hi,

    Give this a shot (you'll need to adjust this if you want it to run fully silently):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            $uninst = $uninst.Replace('{',' ').Replace('}',' ')
            Invoke-Expression $uninst
        }
    
    }

    EDIT: I should mention - Invoke-Expression can be very, very dangerous. Be careful.


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)


    Thanks Mike, no luck with that though. A generic windows installer window pops up asking

    "Are you sure you want to uninstall this product?"

    If I click 'yes' a windows installer error pops up saying

    "This installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid windows Installer package" .

    However, I know it is legitimate because I can run msiexec /x{guid} and the product will uninstall.

    Monday, September 23, 2013 7:32 PM
  • Hmm, I'll have to play around with it a bit more. I didn't actually go through with the uninstall after the box popped up, I just made an assumption that it would work.

    Just another reminder of what happens when I make assumptions...


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)

    Monday, September 23, 2013 7:39 PM
  • Hmm, I'll have to play around with it a bit more. I didn't actually go through with the uninstall after the box popped up, I just made an assumption that it would work.

    Just another reminder of what happens when I make assumptions...


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)

    Ok thanks, also this is going to be put into a foreach($computer in $computers) loop in order to run the script against multiple computers at once, so will need to work silently. I should be able to simply add the switch after the

    Invoke-Expression "$uninst /q /norestart"

    but we'll see.

    Monday, September 23, 2013 7:45 PM
  • Okay, this seems to work for me (testing locally only though):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            & cmd /c $uninst /quiet /norestart
        }
    
    }


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)

    • Marked as answer by commdudeaf Tuesday, September 24, 2013 12:35 PM
    Monday, September 23, 2013 7:56 PM
  • Okay, this seems to work for me (testing locally only though):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            & cmd /c $uninst /quiet /norestart
        }
    
    }


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)


    Mike, That did the trick thanks for the help! I'm not sure how using '&' works to execute cmd but I'll look in to that. Could I use something like Start-Process cmd.exe -ArgumentList "/c $uninst /quiet /norestart" ? I like using start-process since I can use the -Passthru parameter to see the PID it's using, I like to have some feedback. If not then no big deal, this does work as is. Thanks again.
    • Edited by commdudeaf Tuesday, September 24, 2013 2:03 PM
    Tuesday, September 24, 2013 12:35 PM
  • Glad to help. For the & operator, here's some info:

    http://ss64.com/ps/call.html

    As for using Start-Process, this seems to work (I added in a -NoNewWindow so the cmd window doesn't appear):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            Start-Process cmd -ArgumentList "/c $uninst /quiet /norestart" -NoNewWindow
        }
    
    }


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)

    Tuesday, September 24, 2013 2:29 PM
  • Glad to help. For the & operator, here's some info:

    http://ss64.com/ps/call.html

    As for using Start-Process, this seems to work (I added in a -NoNewWindow so the cmd window doesn't appear):

    $javaVer = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |
        Get-ItemProperty |
            Where-Object {$_.DisplayName -match "java" } |
                Select-Object -Property DisplayName, UninstallString
    
    ForEach ($ver in $javaVer) {
    
        If ($ver.UninstallString) {
    
            $uninst = $ver.UninstallString
            Start-Process cmd -ArgumentList "/c $uninst /quiet /norestart" -NoNewWindow
        }
    
    }


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)


    This does work. Just to add for anyone looking at this in the future, you will need to use the -Wait parameter when using Start-Process if running this against remote computers.
    • Edited by commdudeaf Tuesday, September 24, 2013 3:23 PM
    Tuesday, September 24, 2013 2:43 PM
  • Hello,

            I was trying to figure out if the previous mention PS would work for uninstalling my application? The application is called Atex Adbase also Mactive is in there somewhere for the name. I grabbed the following from the registry with Atex installed.

    C:\Program Files (x86)\InstallShield Installation Information\{531BC281-5429-4446-8354-108FCFD2ACC1}\setup.exe -runfromtemp -l0x0009 -removeonly

     

    Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{531BC281-5429-4446-8354-108FCFD2ACC1}

    I'm trying to push this out in a PS script or DOS script. I'm looking for the quickest way to uninstall this application on 200+ machines.

    Thank you,


    Dr. Chuck

    Tuesday, March 4, 2014 7:35 PM
  • Hi,

    This can most likely be adjusted to work with whatever application you're looking to uninstall.

    Please start a new thread though. You can include a link back here if you wish.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Tuesday, March 4, 2014 7:39 PM
  • Is there a way to modify this to look at a CSV with the names of the programs you wish to uninstall?

    If this is helpful please mark it so. Also if this solved your problem mark as answer.

    Wednesday, March 5, 2014 3:54 PM
  • Is there a way to modify this to look at a CSV with the names of the programs you wish to uninstall?

    If this is helpful please mark it so. Also if this solved your problem mark as answer.

    Please start a new thread.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Wednesday, March 5, 2014 3:56 PM
  • thanks that worked!!

    question is there a way to suppress the message "Do you want to completely remove the selected application and all of its features? if i click on Yes it work. but i would like to call my response file using powershell

    Thursday, October 18, 2018 7:28 PM