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: Detect Antivirus Name on a Windows Machine

function getAntivirusName { $wmiQuery = "SELECT * FROM AntiVirusProduct" $antivirus = Get-WmiObject -Namespace "root\SecurityCenter2" -Query…

Linux: Enable PowerShell Remoting WinRM Client on Ubuntu 20.04

This note is a work-in-progress as the NTLM authentication support module by Microsoft for Ubuntu…

PowerShell: Methods to Download Files

# Change these values to reflect your desired downloads$fileURL = "https://download.microsoft.com/download/0/d/9/0d9d81cf-4ef2-4aa5-8cea-95a935ee09c9/PortQryV2.exe"$output = "C:\WINDOWS\System32\SysInternals\portqry.exe"1. Start-BitsTransfer (my…