PowerShell Script to Uninstall Software Easily
Get-WmiObject -Class Win32_Product -Filter "Name='SoftwareName'" | ForEach-Object { $_.Uninstall() } to uninstall software by its exact name in PowerShell.Examples
How to Think About It
Algorithm
Code
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
Get software object
$software = Get-WmiObject -Class Win32_Product -Filter "Name='7-Zip 19.00 (x64)'" returns software object
Check if software found
$software is not null, proceed to uninstall
Call uninstall method
$result = $software.Uninstall() returns ReturnValue = 0
Print success message
Output: Software '7-Zip 19.00 (x64)' found and uninstalled successfully.
| Step | Action | Value |
|---|---|---|
| 1 | Get software object | Software object for '7-Zip 19.00 (x64)' |
| 2 | Check software found | True |
| 3 | Uninstall call result | ReturnValue=0 |
| 4 | Output message | Success 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
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'." }
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." }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| WMI Get-WmiObject | O(n) | O(1) | Simple MSI-registered software |
| Get-Package | O(n) | O(1) | Package-managed software |
| Registry Uninstall Key | O(n) | O(1) | Software not registered with WMI |