Skip to the matchbox tray

How to automate remote software removal with PowerShell

John Martinez 752 views

TL;DR: To perform remote software uninstalls with PowerShell, follow five steps: (1) Discover the right installer using both machine-wide uninstall registry keys; (2) Confirm the installer's silent...

How to automate remote software removal with PowerShell

TL;DR: To perform remote software uninstalls with PowerShell, follow five steps: (1) Discover the right installer using both machine-wide uninstall registry keys; (2) Confirm the installer's silent uninstall command — for MSI installs, use msiexec /x "{ProductCode}" /qn /norestart; (3) Run it remotely using WinRM with approved credentials; (4) Run a staged rollout that includes a pilot; and (5) Recheck the uninstall registry keys to confirm the app's registration is gone.

We've all met it. The Zombie App. The app that refuses to die, no matter how many folders and registry settings you scrub. In this article, we'll see how to nuke these successfully with a repeatable procedure for remote application removal with PowerShell.

How to identify the correct installation and uninstaller

First, find the correct uninstaller; it's in the Windows registry (how'd you guess?).

64-bit apps register their uninstallers under HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall.

32-bit app uninstallers place entries in the HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall path.

Windows Installer apps use the product's GUID as the key name, but other installers (e.g., Inno Setup) may use a different convention.

Don't just match on the name. Ensure the publisher, DisplayName, version, and the MSI product code all line up. Using wildcards here can catch the wrong product. The following examples target machine-wide MSI installations. Run this first, before removing anything, to see which machines have the matching MSI product:

$cred = Get-Credential Invoke-Command -ComputerName (Get-Content .\targets.txt) -Credential $cred ` -ScriptBlock { Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Where-Object { $_.DisplayName -eq 'Example App' -and $_.Publisher -eq 'Example Corp' -and $_.WindowsInstaller -eq 1 } | Select-Object DisplayName, DisplayVersion, PSChildName }

Hot tip: Avoid using the Win32_Product class in Windows Management Instrumentation, as it verifies and repairs installs. Microsoft itself says this method is slow and can produce side effects.

If you want to run a silent software uninstall, you'll need to find the correct installer-specific switches to enable it, which will differ for third-party (non-MSI) installers. For MSI installs, a silent uninstall closes a user's app without prompting. To plan for this, define a maintenance window and ask users to save their work before the uninstall.

Remote software uninstall with PowerShell across multiple devices

First, ensure that WinRM is correctly configured on clients and that you're familiar with using remoting commands through WinRM. Use the code below, which assumes you've already populated $cred:

$code = '{11111111-2222-3333-4444-555555555555}' $log = foreach ($pc in Get-Content .\pilot.txt) { try { $exit = Invoke-Command -ComputerName $pc -Credential $cred ` -ErrorAction Stop -ScriptBlock { (Start-Process msiexec.exe ` -ArgumentList "/x $using:code /qn /norestart" ` -Wait -PassThru).ExitCode } $state = switch ($exit) { 0 { 'Verify' } { $_ -in 3010, 1641 } { 'RestartRequired' } 1605 { 'AbsentInContext' } 1618 { 'Retry' } default { 'Failed' } } } catch { $exit = $null; $state = 'Unreachable' } [pscustomobject]@{ Computer = $pc; Product = $code; ExitCode = $exit State = $state; Time = Get-Date -Format s } } $log | Export-Csv .\uninstall-log.csv -Append -NoTypeInformation

Test this on a representative group of devices so you're not bricking the entire company's laptops with runaway code.

Finally, check that the app's uninstall registration is gone from both machine-wide uninstall registry locations:

Invoke-Command -ComputerName (Get-Content .\pilot.txt) -Credential $cred ` -ScriptBlock { Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$using:code", "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\$using:code" }

How to verify removal and handle exceptions

After the pilot run, review each Windows Installer outcome from your log and handle appropriately:

Result

What it means

What to do

Unreachable

The host is offline, no network path exists, WinRM is off, or credentials are wrong

Fix, then retry

Absent in context

Code 1605: Indicates the action is only valid for currently installed products, even though discovery found a match

Ensure you have the right product code. If you do, this may be a per-user install that a SYSTEM/remoting account can't uninstall

Restart required

Either a 3010 (restart required), often because an app is busy or still in use, or a 1641 (restart initiated). Both indicate success

For 3010, schedule a restart, then verify. For 1641, wait for the machine to restart, then verify.

Retry

Another install is in progress (1618)

Retry later

Failed

An error not handled above, such as 1603 (fatal error)

Check the MSI log (rerun with /l*v)

Verify

Success

Run the verify check

How to remove software remotely with PDQ

If WinRM isn't an option, or you need to reach machines on home networks, PDQ can run the tested uninstall command locally through the PDQ agent over the internet, no VPN required. Offline devices will be queued and processed once they reconnect.

You can also skip writing your own removal script. The PDQ Uninstall It All template in the Package Library includes a script that finds the application you specify and attempts to uninstall it. Duplicate the package so you can edit its parameters, then run it with the default -AuditOnly parameter to confirm the app’s exact name without removing anything. In the Parameters field, replace -AuditOnly with that name in quotation marks (for example, "Example App"). Deploy to your pilot group and verify removal before expanding the rollout.

If you need to use your own tested uninstall command, create a custom package:

1. Write the removal script as a custom PDQ package (Packages > Create package), with the uninstall command as a script step. For a PowerShell step, use the following local script, substituting the actual product code, so the MSI exit code is returned to PDQ:

$code = '{11111111-2222-3333-4444-555555555555}' $process = Start-Process -FilePath 'msiexec.exe' ` -ArgumentList "/x $code /qn /norestart" ` -Wait -PassThru -ErrorAction Stop exit $process.ExitCode

2. In Success codes, list 0, 3010, and 1641 to cover removal and pending-restart cases. PDQ will treat any unlisted code as an error.

3. Leave Run as on Local System for machine-wide installs; switch to Logged on User for the per-user installs that return a 1605 (users must be logged in directly, not via Remote Desktop).

4. Target a device group built from PDQ's software inventory, deploy to the pilot group, then review and rescan to confirm app removal.

PDQ handles the delivery; you'll still need to debug install contexts and per-vendor anomalies.

Remote software uninstall with PowerShell FAQs

Why not use Uninstall-Package?

Uninstall-Package depends on the package provider. The built-in Programs provider can list EXE-installed apps through Get-Package but does not reliably uninstall them. Use the vendor’s uninstall command instead.

Can PowerShell uninstall per-user apps remotely?

Yes. Run the appropriate uninstaller as the user who owns the installation. For per-user MSI installs, use msiexec /x "{ProductCode}" /qn /norestart. In PDQ, use Logged on User while that user is locally logged in.

ConnectIcon CTA

Manage Windows & macOS devices from anywhere

With PDQ Connect, get real-time visibility into remote and local devices, deploy software, remediate vulnerabilities, automate routine maintenance, and remotely troubleshoot endpoints from one easy-to-use platform.

Source: John Martinez · www.pdq.com

From the Same Series