Microsoft offers several options if you want to remove installed programs by using the command line or a script. Unfortunately, no method covers all scenarios. PowerShell's package management, for example, is limited to the local computer, while WMI does not recognize all programs.

Even when uninstalling Windows components, there is an unnecessary variety of options, such as Remove-WindowsCapability, Remove-WindowsFeature, and Disable-WindowsOptionalFeature. In addition, there is Remove-WindowsPackage from the DISM module.

CLI options for uninstalling applications

The command line tools for uninstalling programs are similarly inconsistent. A few years ago, Microsoft introduced PowerShell Package Management, which is primarily for adding and removing PowerShell modules. However, it is also suitable for uninstalling Win32 programs.

Then, in 2020, the package manager winget was added. A year later, it offered the ability to remove programs. However, it still does not have a PowerShell interface, and thus is limited in its options when automating package management.

In addition, separate cmdlets exist for Store and UWP apps with Remove-AppxProvisionedPackage and Remove-AppxPackage.

Finally, it is possible to uninstall applications using WMI. This is the only mechanism mentioned here that can perform this task remotely.

Uninstalling programs via WMI

The class Win32_Product is responsible for this. With its help, you can first view the installed software:

Get-CimInstance -Class Win32_Product -ComputerName <Remote-PC> |
-Format-List

Once you have viewed the program in question, you can further narrow the list:

Get-CimInstance -Class Win32_Product -ComputerName <Remote-PC> |
where name -like " PowerShell*"

If you send the output of this command through a pipe to Get-Member, you will not find an uninstall method. To get it, you have to use the older Get-WMIObject instead:

$app = Get-WmiObject -Class Win32_Product -ComputerName <Remote-PC> |
where name -Like "PowerShell*"

In our example, this call captures an outdated version of PowerShell 7 and assigns the result to the $app variable.

Uninstalling the program remotely via WMI and PowerShell

Uninstalling the program remotely via WMI and PowerShell

Then, uninstall the program with

$app.uinstall()

In general, this method works quite reliably and uninstalls applications remotely. However, it is hampered by the fact that the installed programs usually cannot be displayed completely via WMI.

The comparison of Win32_Product and Get Package shows that WMI does not list all programs

The comparison of Win32_Product and Get Package shows that WMI does not list all programs

Tools like 7-zip don't appear at all, and others appear only with a GUID, so you don't know what the entry stands for. In this case, you can switch to package management, which only works locally.

Remove programs with uninstall-package

To list the installed programs, use the Get-Package cmdlet. However, this also shows standalone updates (msu) or modules installed with PowerShellGet. Therefore, you can filter the output for traditional Win32 applications as follows:

Get-Package -ProviderName Programs -IncludeWindowsInstaller

Once you have found the program in question, specify it using the Name parameter and pass it to Uninstall-Package:

Get-Package -Name "7-zip*" | Uninstall-Package

Get-Package also enables filtering programs by their version number and uninstalling only certain versions (for example, "RequiredVersion") or all versions ("AllVersions").

Conclusion

Microsoft offers quite a jumble of program removal techniques. None of them covers the full range of required features. Either they are limited to certain types of programs or are incapable of removing programs remotely.

Subscribe to 4sysops newsletter!

The latter can be done via WMI, although you can't use the new CIM cmdlets for that. If you only get an incomplete list of installed software this way, then you can use the cmdlets from the PackageManagement package—but only locally.

avataravataravataravatar
5 Comments
  1. Jonathan Seymour 1 year ago

    Unless PowerShell is different somehow, the general consensus is that using Win32_Product to uninstall programs is a BAD idea:

    https://xkln.net/blog/please-stop-using-win32product-to-find-installed-software-alternatives-inside/

  2. david 1 year ago

    How I can uninstall without reboot?

  3. Dustan Korte 3 months ago

    I will leave this little powershell script gem here:

    $Program = get-wmiobject -class Win32_Product -filter "name like 'duo%'" | format-table -HideTableHeaders -property IdentifyingNumber
    $Uninstall = 'msiexec /x /q'
    Start-Process $Uninstall "$Program"

  4. Don’t just run Get-WMIObject or Get-CIMInstance against win32_product – it will trigger automatic repairs against the products that have been “touched”. This is virtually always a bad idea. You’re much better off just searching the registry. This particular function works really well, and is very fast. https://gist.github.com/indented-automation /32efb05a5fb67ef9eed02bbb8fe90691 Most of the applications will normally also include an UninstallString entry – it should include the full msiexec command line to uninstall the application.

    David F.

  5. The URL I copied for Chris Dent’s Get-InstalledSoftware got chopped in half in the text. Make sure you copy the entire link up to the 691 at the end of the URL.

    David F.

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account