Uninstall app with PowerShell

Uninstall app with PowerShell

I recently found myself in a situation on a client’s device where I needed to remove an application, but I was not a local administrator on the device. After evaluating options (like the slow WMI client), I turned to PowerShell. PowerShell provides a simple and fast way to search for and uninstall MSI-installed applications.

Skip to the code!

After exploring a few options, including the glacially slow WMI client, I decided to turn to my old friend PowerShell. And voilà! PowerShell provides a simple and fast way to search for and remove applications.

On to the code

To run this, open PowerShell as an Administrator and run the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function Uninstall-App {
<#
    .SYNOPSIS
        Uninstalls a Windows application (MSI provider) by searching for the correct app.

    .DESCRIPTION
        This function uses the Filter parameter to search through Windows applications 
        for the targeted app. If the parameter is not provided, the user will 
        be prompted. The function will only offer to uninstall if the search 
        returns one app.

    .PARAMETER Filter
        A string that is used to search application based on Name or TagId.

    .EXAMPLE
        Uninstall-App -Filter "Node.js"
#>
    [CmdletBinding()]
    Param (
        [string]$Filter
    )

    Process {
        Clear-Host
        
        if ([string]::IsNullOrWhiteSpace($Filter)) {
            $Filter = Read-Host -Prompt "Enter the app Name or TagId to search for"
            if ([string]::IsNullOrWhiteSpace($Filter)) {
                Write-Host "`r`nNo search criteria entered. Exiting." -ForegroundColor Yellow
                return
            }
        }
        
        Write-Host "`r`nSearching for '$Filter' ..." -ForegroundColor Cyan
        
        $apps = Get-Package | Where-Object { 
            ($_.Name -like "*$Filter*" -or ($_.TagId -and $_.TagId -like "*$Filter*")) -and 
            $_.ProviderName -eq "msi" 
        } | Select-Object Name, Version, TagId
        
        if (-not $apps -or $apps.Count -eq 0) {
            Write-Host "`r`nYour search returned no results." -ForegroundColor Yellow
            return
        }
        
        if ($apps.Count -gt 1) {
            Write-Host "`r`nYour search returned more than one app, but only one app can be uninstalled at a time. Please adjust the filter and try again." -ForegroundColor Yellow
            Write-Host "HINT: Use the TagId to filter your search." -ForegroundColor Yellow
            Write-Host "`r`nApps that match your request:" -ForegroundColor Yellow
            $apps | Sort-Object Name | Format-Table Name, TagId, Version -AutoSize
            return
        }
        
        $apps[0] | Format-Table Name, TagId, Version -AutoSize
        $confirm = Read-Host -Prompt "Confirm you want to remove the application listed above (Y/n)"
        
        if ($confirm -eq '' -or $confirm -match '^(?i)y(es)?$') {
            Write-Host "`r`nUninstalling ... " -ForegroundColor Green
            try {
                Uninstall-Package -Name $apps[0].Name -RequiredVersion $apps[0].Version -Force
                Write-Host "Uninstall command completed." -ForegroundColor Green
            }
            catch {
                Write-Host "Uninstall failed: $($_.Exception.Message)" -ForegroundColor Red
            }
        }
        else {
            Write-Host "Exiting." -ForegroundColor Yellow
        }
    }
}

Uninstall-App