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: Checking Duplicating Identifiers Among ADFS Relying Party Trusts

function getDuplicatingIfd{ write-host "Checking each relying party trust for any duplicates of identifiers..." $trusts=Get-AdfsRelyingPartyTrust $allTrustNames=$trusts.Name…

PowerShell: Create Registry Keys within Windows Localhost

# createRegKey.ps1 $regKeys=@( @{ hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome'; name='ChromeCleanupEnabled'; value=0 } @{ hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome'; name='ChromeCleanupReportingEnabled'; value=0 } ) function…

PowerShell: Improve Network Speed of Windows on 20 Mbps or Faster Connections

This has been tested on Windows 10 - will not work on a Server OS:…