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

PowerShell: Get Executable Version and File Location

# Quick method to obtain computernames of all nodes in a cluster and adjoin result…

PowerShell: Comparing 2 Directories

# Set source and destination$source="C:\temp"$destination="C:\tempcopy"# Mirror the 2 directoriesrobocopy $source $destination /MIR /R:0 /NP# create…

PowerShell: Optimize SQL Server Memory & CPU Resources

# optimizeSqlServer.ps1 # Version 0.0.2 # This version deals with Memory, CPU # Future versions…