0

I'm trying to uninstall Java using PowerShell using the code below. I want the powershell to look into both HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall* and HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall* and then uninstall.

For some reason, it only pulls from $source2 and not from $source. Can someone please assist me with what I'm missing here? I'd really appreciate it. $Source gets the value for Java 32-bit and $Source2 picks up the value for Java 64-bit.

$Source = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
$source2 = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*
$Uninstall = $Source | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')} | select UninstallString
$Uninstall = $Source2 | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')} | select UninstallString
$UninstallStrings = $Uninstall.UninstallString -replace "MsiExec.exe ","MsiExec.exe /qn " -replace "/I","/X"
ForEach($UninstallString in $UninstallStrings){& cmd /c "$UninstallString"}
Sam
  • 115
  • 2
  • 9

1 Answers1

1

It looks like you are overwriting your $Uninstall variable on your fourth line; your code only pulls from $source2 because you overwrite the UninstallString array from $Source with the UninstallString array from $source2.

You could try changing your code to use the += operator to append the UninstallString array you get on line 4 to the array you get on line 3 like this.

$Uninstall += $Source2 | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')} | select UninstallString

However, you might run into trouble if the registry keys returned by ($Source | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')}) lack any UninstallString properties (as they do on my system). A better approach might be to first concatenate $Source and $source2 like this:

$Uninstall = ($Source + $source2) | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')} | select UninstallString
fakedad
  • 1,292
  • 1
  • 10
  • 21
  • Oh that'll work!!! I'll test it out and let you know. Do you know how I can output the logs to log.txt. in case Uninstaller fails. – Sam Sep 22 '21 at 23:41
  • @Sam I'm not sure what the best way to log the uninstall process is. You might look at [this question](https://stackoverflow.com/questions/7126077/create-an-msi-log-file). I'm hesitant to test, though, since I don't want to uninstall Java on my own system. – fakedad Sep 22 '21 at 23:45
  • Thank you! I ended up using ($Source + $source2) and that worked for me! – Sam Sep 23 '21 at 02:26
  • @Sam You're welcome. I'm glad it worked for you. – fakedad Sep 23 '21 at 02:31
  • 1
    FWIW, when I did this, I grabbed the product ID from the registry path using $id = $key.Name | Split-Path -Leaf and then used that to build my own uninstall string that included a log of the uninstall: $process = Start-Process -FilePath 'msiexec.exe' -ArgumentList "/x $id /qn /l*xv ""c:\windows\temp\$displayName-uninstall.log""" -PassThru -Wait. Once that completed, I checked $process.ExitCode to report success (0) or failure (non-0). – DarkMoon Sep 23 '21 at 02:56