Uninstalling a Package with Winget

What

Winget is a command line tool to manage applications on the Windows 10 and 11 platform.

When/Why

After we initially install any package, we most likely will have reason to uninstall it at some point...and we can use this very practically in a pre or post-uninstall script within a payload/fileset.

How

The process for removing a package is very similar to our installation and upgrade code, and equally simple.  We'll use the winget "uninstall" command to remove a previously installed package.  You'll find Microsoft documentation on the update command here.  And the uninstall command is shown in action in the last line below:

# because we run this from system account, we must define where winget is...the registry tells us
$winget_locale=Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\winget.exe" -Name Path -ErrorAction SilentlyContinue

# we change directory to this locale so we can run the winget command without the full path
cd $winget_locale.Path

# uninstall doesn't have agreements, so we can exclude those options
# see special note on the meaning of "silent"
./winget uninstall --id Mozilla.Firefox --silent

Utilization of the winget list command may also be very helpful in finding out which packages are currently installed.  You can find details on that command here.

"Silent" doesn't necessarily mean silent in all cases.  This winget process just uses normal install and uninstall options from third parties, but in an easy-to-use package.  What our testing has found though is that the uninstall will only be "silent" if the 3rd party includes a QuietUninstallString in HKLM/Software/Microsoft/Windows/CurrentVersion/Uninstall.  In our case above, Mozilla provides no such string...they only provide an UninstallString, which does prompt.  So, "silent" in this case is not silent.  As always, testing of these items before production rollout is always recommended.

Digging Deeper

You may be asking yourself can I make the uninstall silent if it isn't?  Yes, probably.  In the case of Firefox for instance, the UninstallSring calls an executable called helper.exe.  That executable though, when run with a /S flag, will actually uninstall the application silently.  So the command to do this could be place into either the pre or post-uninstall script on the fileset to accomplish instead of using the winget command.