0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Uninstall Software Easily

Use Get-WmiObject -Class Win32_Product -Filter "Name='SoftwareName'" | ForEach-Object { $_.Uninstall() } to uninstall software by its exact name in PowerShell.
📋

Examples

InputUninstall '7-Zip 19.00 (x64)'
OutputSoftware '7-Zip 19.00 (x64)' found and uninstalled successfully.
InputUninstall 'NonExistentApp'
OutputNo software found with the name 'NonExistentApp'.
InputUninstall 'Mozilla Firefox'
OutputSoftware 'Mozilla Firefox' found and uninstalled successfully.
🧠

How to Think About It

To uninstall software using PowerShell, first find the installed program by its exact name using Windows Management Instrumentation (WMI). Then call the uninstall method on the found program object. This approach works well for software registered with Windows Installer.
📐

Algorithm

1
Get the software name as input.
2
Search installed software using WMI with a filter on the software name.
3
If software is found, call the uninstall method on it.
4
If no software is found, notify the user.
5
Print the result of the uninstall operation.
💻

Code

powershell
param([string]$SoftwareName)

$software = Get-WmiObject -Class Win32_Product -Filter "Name='$SoftwareName'"

if ($software) {
    $result = $software.Uninstall()
    if ($result.ReturnValue -eq 0) {
        Write-Output "Software '$SoftwareName' found and uninstalled successfully."
    } else {
        Write-Output "Failed to uninstall '$SoftwareName'. Return code: $($result.ReturnValue)"
    }
} else {
    Write-Output "No software found with the name '$SoftwareName'."
}
🔍

Dry Run

Let's trace uninstalling '7-Zip 19.00 (x64)' through the code

1

Get software object

$software = Get-WmiObject -Class Win32_Product -Filter "Name='7-Zip 19.00 (x64)'" returns software object

2

Check if software found

$software is not null, proceed to uninstall

3

Call uninstall method

$result = $software.Uninstall() returns ReturnValue = 0

4

Print success message

Output: Software '7-Zip 19.00 (x64)' found and uninstalled successfully.

StepActionValue
1Get software objectSoftware object for '7-Zip 19.00 (x64)'
2Check software foundTrue
3Uninstall call resultReturnValue=0
4Output messageSuccess message printed
💡

Why This Works

Step 1: Find software by name

The Get-WmiObject command queries installed software matching the exact Name property.

Step 2: Call uninstall method

The Uninstall() method on the software object triggers the uninstall process.

Step 3: Check uninstall result

The method returns a code; 0 means success, other values indicate failure.

🔄

Alternative Approaches

Using Get-Package and Uninstall-Package
powershell
param([string]$SoftwareName)

$pkg = Get-Package -Name $SoftwareName -ErrorAction SilentlyContinue
if ($pkg) {
    Uninstall-Package -InputObject $pkg -Force
    Write-Output "Software '$SoftwareName' uninstalled via Get-Package."
} else {
    Write-Output "No package found with the name '$SoftwareName'."
}
Works for software installed via package providers like MSI or Chocolatey; requires PowerShell 5.0+.
Using registry uninstall keys
powershell
param([string]$SoftwareName)

$uninstallKey = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall'
$apps = Get-ChildItem $uninstallKey | ForEach-Object {
    Get-ItemProperty $_.PSPath
} | Where-Object { $_.DisplayName -eq $SoftwareName }

if ($apps) {
    $uninstallString = $apps.UninstallString
    if ($uninstallString) {
        Start-Process -FilePath "cmd.exe" -ArgumentList "/c", "$uninstallString /quiet" -Wait
        Write-Output "Software '$SoftwareName' uninstalled via registry uninstall string."
    } else {
        Write-Output "Uninstall string not found for '$SoftwareName'."
    }
} else {
    Write-Output "No software found with the name '$SoftwareName' in registry."
}
More complex but works for software not registered with WMI; requires admin rights.

Complexity: O(n) time, O(1) space

Time Complexity

The script queries installed software which is a list of size n; searching by name is O(n) in the worst case.

Space Complexity

The script stores only the matched software object and result, so space is constant O(1).

Which Approach is Fastest?

Using WMI is straightforward but can be slower; querying registry keys is faster but more complex; Get-Package depends on installed providers.

ApproachTimeSpaceBest For
WMI Get-WmiObjectO(n)O(1)Simple MSI-registered software
Get-PackageO(n)O(1)Package-managed software
Registry Uninstall KeyO(n)O(1)Software not registered with WMI
💡
Run PowerShell as administrator to ensure uninstall commands have the needed permissions.
⚠️
Using partial software names in the filter can cause no matches; always use the exact software name.