Posted On January 30, 2023

PowerShell: Uninstall Program Using Its Name

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> PowerShell: Uninstall Program Using Its Name
# uninstallProgramUsingId.ps1

$appName='Virtual Machine Manager Agent'
$uninstallStringRegPaths='HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall','HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$uninstallStrings=Get-ChildItem -Path $uninstallStringRegPaths
$uninstallString=($uninstallStrings|Get-ItemProperty|Where-Object {$_.DisplayName -match $appName}).UninstallString
if($uninstallString.count -eq 1){
    $appCode=[regex]::match($uninstallString,'\{(.*)\}').Value
    $uninstallCommand="& msiexec.exe /x $appCode /quiet /norestart"
    write-host "Invoking uninstall Command: $uninstallCommand"
    Invoke-Expression $uninstallCommand
    $appStillExists=Get-WmiObject -Class Win32_Product -Filter "Name='$appName'"
    if($appStillExists){
        write-warning "Uninstall has been unsuccessful at removing $appName"
        return $false
    }else{
        return $true
    }
}else{
    write-warning "Please check this/these uninstall string(s):`r`n$uninstallString"
    return $false
}

Leave a Reply

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

Related Post

PowerShell: Quick Exercise On Disks Operations

# View disks on the system get-disk Number Friendly Name Serial Number HealthStatus OperationalStatus Total…

PowerShell: Adding Active Directory Module

Windows environments often are controlled by Active Directory; hence, it is useful to include this…

Install FOG, an Open Source Machine Backup and Cloning Solution

- Install Ubuntu or Centos - Install FOG - On Windows Client, run sysprep, then…