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:
# Get the path to winget.exe
$winget = (Get-ChildItem -Path "C:\Program Files\WindowsApps\" -Filter "winget.exe" -Recurse | Sort-Object -Property LastWriteTime | Select-Object -Last 1).FullName
# Get the directory that winget.exe is inside of
$wingetdir = Split-Path -Path $winget -Parent
# we change directory to this locale so we can run the winget command without the full path
cd $wingetdir
# uninstall doesn't have agreements, so we can exclude those options
# see special note on the meaning of "silent"
.\winget.exe uninstall --exact --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 uninstall string 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.