## Get all of the uninstall registry keys $UNINSTALL_KEYS = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall $UNINSTALL_KEYS += Get-ChildItem -Path HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall ## Create an empty list to store all of the MSI operations $MSI_OPS = @{} Foreach ( $reg in $UNINSTALL_KEYS ) { ## Get each uninstall key $reg_property = Get-ItemProperty -Path $reg.PSPath ## Make sure the DisplayName matches "Java 8" or "Java 7" If ( ($reg_property.DisplayName -ilike "Java* 8 *") -or ($reg_property.DisplayName -ilike "Java* 7 *") ) { ## Make sure the software was installed using an MSI If ( $reg_property.WindowsInstaller -eq 1 ) { ## Placeholder to make sure we add the current item to $MSI_OPS $ADD_ITEM = $true ## Get the product code from the UninstallString If ( $reg_property.UninstallString ) { ## Strip away the command part of UninstallString and only return the MSI product code $PRODUCT_CODE = $reg_property.UninstallString.Replace('MsiExec.exe /X','') } ## Get the product code from the ModifyPath ElseIf ( $reg_property.ModifyPath ) { ## Strip away the command part of ModifyPath and only return the MSI product code $PRODUCT_CODE = $reg_property.ModifyPath.Replace('MsiExec.exe /X','') } ## Since we can't get a product code, make sure we don't add this item to $MSI_OPS Else { $ADD_ITEM = $false } ## Add the DisplayName and $PRODUCT_CODE to $MSI_OPS If ( $ADD_ITEM ) { $MSI_OPS.Add( $reg_property.DisplayName, $PRODUCT_CODE ) } } } } ## Uninstall each of the products in $MSI_OPS Foreach ( $app in $MSI_OPS.Keys ) { Write-Output $app $args = '/X{0} /qn /norestart' -f $MSI_OPS[$app] Start-Process -FilePath "msiexec.exe" -ArgumentList $args -Wait } # Delete lingering directories and binary files If ( Test-Path "${Env:ProgramData}\Oracle\Java" ) { Write-Output "Deleting ${Env:ProgramData}\Oracle\Java\*" Try { Remove-Item -Path "${Env:ProgramData}\Oracle\Java" -Recurse -Force } Catch { Write-Output $_ } }