Posted On April 28, 2021

PowerShell: Uninstalling an Application

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Uninstalling an Application
$appname='Google Chrome'

function uninstallApp($appName){
  $alternativeMethod=$false
  try{
    $app = Get-WmiObject -Class Win32_Product | Where-Object{$_.Name -eq $appName}
    $app.Uninstall()
  }catch{
    write-warning $_
    $alternativeMethod=$true
  }
  if($alternativeMethod){
    try{
      $uninstallStrings = (Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall|
      Get-ItemProperty|Where-Object {$_.DisplayName -match $appName}).UninstallString
      ForEach ($uninstallString in $uninstallStrings) {
              $uninstallCommand = $uninstallString.Replace('{',' ').Replace('}',' ')+'ACCEPT=YES /qr+'
              write-host "Attempting alternative method of invoking: $uninstallCommand"
              Invoke-Expression $uninstallCommand
      }
      return $true
    }catch{
      return $false
    }
  }else{
    return $true
  }
}

uninstallApp $appname

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How To Invoke Functions as Background Jobs

Invoke-Command, Start-Job, Multi-Tasking is what good coders should aspire toward. Here, we're looking at some…

A Broken PS-Session Issue

Symptoms: PS C:\Windows\system32> get-pssession Id Name ComputerName ComputerType State ConfigurationName Availability -- ---- ------------ ------------…

Windows: How to Map Network Drive for All Users of a Local Machine

# Mapping file share using legacy commands (more effective than newer PowerShell equivalents in certain…