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"}
CodePudding user response:
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