c4os
c4os

Reputation: 29

Powershell: how do I detect if an MSI package is installed or not before removing an EXE?

I made a PS script to remove a software called Jabra. It works fine, finding the two entries:

DisplayName  UninstallString                                                                                       
-----------  ---------------                                                                                       
Jabra Direct "C:\ProgramData\Package Cache\{b1b65c84-0885-49ea-bee4-b9fd0b1c5ce7}\JabraDirectSetup.exe"  /uninstall
Jabra Direct MsiExec.exe /I{C5DCA8EB-FFEC-485B-84F1-924425979106}

however here's the problem: the MSI needs to be removed before the EXE or the EXE fails. Is there a way to add a check or something for the EXE to check that the MSI has been removed before running?

Thanks,

Ste

$UninstJabra = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall  |  Get-ItemProperty | Where-Object {$_.DisplayName -eq "Jabra Direct" } | Select-Object -Property DisplayName,UninstallString 

ForEach ($app in $UninstJabra){
if ($app.UninstallString -like "msiexec.exe*")
{
write-host "uninstalling $($app.DisplayName)....msi"
$app.UninstallString = $app.UninstallString -Replace "msiexec.exe","" -Replace "/I",""
$app.UninstallString = $app.UninstallString.Trim()
Start-Process msiexec.exe -ArgumentList "/X $($app.UninstallString) /qn" -wait
}
else
{
write-host "uninstalling $($app.DisplayName)....exe"
cmd /c $UninstJabra.UninstallString /silent
}
}

edit

get-package *jabra*

gives me this output

Name                           Version          Source                           ProviderName                               
----                           -------          ------                           ------------                               
Jabra Direct                   4.0.8560.0                                        msi                                        
Jabra Direct                   4.0.8560.0                                        Programs

edit 2

Sorted this way, thank you everyone

$UninstJabra = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -eq "Jabra Direct" } | Select-Object -Property DisplayName, UninstallString | Sort-Object -Property UninstallString -Descending

ForEach ($app in $UninstJabra){
if ($app.UninstallString -like "msiexec.exe*") {
write-host "uninstalling $($app.DisplayName)....msi"
$app.UninstallString = $app.UninstallString -Replace "msiexec.exe","" -Replace "/I",""
$app.UninstallString = $app.UninstallString.Trim()
Start-Process msiexec.exe -ArgumentList "/X $($app.UninstallString) /qn" -wait
}
else {
 #if ($app.UninstallString -like "*ProgramData*") {
write-host "uninstalling $($app.DisplayName)....exe"
$UninstJabra = Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -eq "Jabra Direct" } | Select-Object -Property DisplayName, UninstallString
cmd /c $UninstJabra.UninstallString /silent
 #}
}
}

changed the order for the get-childitem to put MSI on top

Upvotes: 0

Views: 2879

Answers (2)

js2010
js2010

Reputation: 27491

I assume it will output both program and msi providers. You can use uninstall-package, but only with msi's. This only works in powershell 5, not above yet.

if (get-package 'jabra direct' -provider msi) { 'msi installed' } 

Upvotes: 0

wasif
wasif

Reputation: 15488

To check if an application is installed then try this:

if (Get-WMIObject Win32_Product -ErrorAction SilentlyContinue | Where-Object {$_.Name -match "Jabra"}) {
  # Package exists
} 
Else {
  # Package does not exist
}

Upvotes: 3

Related Questions