Skip to main content

Winget Uninstall Application

Summary

This document outlines the process to uninstall an application via Winget.

Parameters

ID = Winget application ID (Example: Google.Chrome)
To obtain the ID, you can search in the command prompt using winget search appname or by browsing to winget.run.

Dependencies

None. This script can be executed on any Windows device.

Create Script

To implement this script, please create a new PowerShell-style script on the system.

Winget Uninstall Application
Winget Uninstall Application

Name: Winget Uninstall Application
Description:

Attempts to uninstall an application via Winget  
Parameter:
- ID = Winget application ID (Example: Google.Chrome)
To obtain the ID, you can search in the command prompt using `winget search appname` or by browsing to winget.run.

Category: Custom

Parameter

Parameter

Parameter
Parameter Name: id
Required Field: Selected
Parameter Type: Text String

Script

Row 1 Function: Script Log

Script Log

Input the following:

Id parameter = @id@.
Attempting to uninstall @id@...

Input

Row 2 Function: PowerShell Script

PowerShell Script

Paste the following PowerShell script and set the expected time of script execution to 600 seconds.

#region parameters
$packageId = '@id@'

$Parameters = @{
'Uninstall' = $true
'PackageId' = $packageId
}
#endRegion
#region Setup - Variables
$ProjectName = 'Invoke-WingetProcessor'
[Net.ServicePointManager]::SecurityProtocol = [enum]::ToObject([Net.SecurityProtocolType], 3072)
$BaseURL = 'https://file.provaltech.com/repo'
$PS1URL = "$BaseURL/script/$ProjectName.ps1"
$WorkingDirectory = "C:\ProgramData\_automation\script\$ProjectName"
$PS1Path = "$WorkingDirectory\$ProjectName.ps1"
$Workingpath = $WorkingDirectory
$LogPath = "$WorkingDirectory\$ProjectName-log.txt"
$ErrorLogPath = "$WorkingDirectory\$ProjectName-Error.txt"
#endRegion
#region Setup - Folder Structure
New-Item -Path $WorkingDirectory -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
$response = Invoke-WebRequest -Uri $PS1URL -UseBasicParsing
if (($response.StatusCode -ne 200) -and (!(Test-Path -Path $PS1Path))) {
throw "No pre-downloaded script exists and the script '$PS1URL' failed to download. Exiting."
} elseif ($response.StatusCode -eq 200) {
Remove-Item -Path $PS1Path -ErrorAction SilentlyContinue
[System.IO.File]::WriteAllLines($PS1Path, $response.Content)
}
if (!(Test-Path -Path $PS1Path)) {
throw 'An error occurred and the script was unable to be downloaded. Exiting.'
}
#endRegion
#region Execution
if ($Parameters) {
Write-Information ('Parameters Used: {0}' -f ($Parameters | Out-String)) -InformationAction Continue
& $PS1Path @Parameters
} else {
& $PS1Path
}
#endRegion
#region log verification
if ( !(Test-Path $LogPath) ) {
throw 'PowerShell Failure. A Security application seems to have restricted the execution of the PowerShell Script.'
}
if ( Test-Path $ErrorLogPath ) {
$ErrorContent = ( Get-Content -Path $ErrorLogPath )
throw ('Error Content: {0}' -f ($ErrorContent | Out-String))
}
$content = Get-Content -Path $LogPath
$logContent = $content[ $($($content.IndexOf($($content -match "$($ProjectName)$")[-1])) + 1)..$($Content.length - 1) ]
return ('Log Content: {0}' -f ($logContent | Out-String))

Script Log

Row 3 Function: Script Log

Script Log

In the script log message, simply type %output% so that the script will send the results of the PowerShell script above to the output on the Automation tab for the target device.

Output

The final task should look like the screenshot below.
Final Task

Script Deployment

The script is intended to run manually at this time.

Output

  • Script log