- Join Windows 11 to an Active Directory domain - Thu, Jun 1 2023
- Change Windows network profiles between public and private - Wed, May 24 2023
- How to map a network drive with PowerShell - Wed, May 17 2023
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.
Then, uninstall the program with
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.
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").
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.
Join the 4sysops PowerShell group!
Your question was not answered? Ask in the PowerShell forum!
Unless PowerShell is different somehow, the general consensus is that using Win32_Product to uninstall programs is a BAD idea:
How I can uninstall without reboot?
I will leave this little powershell script gem here:
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.
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.