Skip to main content

Scheduled Task Winget Update All (Delete)

Summary

The ultimate objective of the task is to remove the scheduled tasks (Winget Update All [System] and Winget Update All [Logged on User]) created by the Scheduled Task Winget Update All (Create) task.

Sample Run

Sample Run 1

Sample Run 2

Dependencies

Variables

NameDescription
OutputOutput of the command removing the scheduled tasks

Task Creation

Create a new Script Editor style script in the system to implement this task.

Task Creation 1

Task Creation 2

Name: Scheduled Task Winget Update All (Delete)

Description: The ultimate objective of the task is to remove the scheduled tasks (Winget Update All [System] and Winget Update All [Logged on User]).

Category: Patching

Task Creation 3

Task

Start by adding a row. You can do this by clicking the Add Row button at the bottom of the script page.

Task Step 1

This function will appear.

Task Step 2

Row 1 Function: PowerShell Script

Search and select the PowerShell Script function.

Row 1 Step 1

Row 1 Step 2

Paste in the following PowerShell script and set the expected time of script execution to 300 seconds. Click the Save button.

if ( (get-ciminstance -classname Win32_OperatingSystem).caption -match 'Windows 1[01]' ) { 'Supported' } else { 'Unsupported' }

Row 1 Step 3

Row 2 Logic: If Then

Row 2 Step 1

Row 2 Step 2

Row 2a Condition: Output Contains

Type Unsupported in the Input Value or Variable field and press Enter.

Row 2a Step 1

Row 2b Function: Script Log

Add a new row by clicking on the Add row button.

Row 2b Step 1

Search and select the Script Log function.

Row 2b Step 2

Add the following log in the Script Log Message field and click the Save button:

Unsupported Operating System.

Supported OS are Windows 10 and Windows 11

alt text

Row 2c Function: Script Exit

Add a new row by clicking on the Add row button.
alt text

Search and select the Script Exit function.
alt text

Note: Do not add any message or words in the Error Message field. Leave it blank and click the Save button.

Row 3 Function: Command Prompt Script

Add a new row by clicking on Add row button outside the If/Then logic.
alt text
alt text

Search and select the Command Prompt (CMD) Script function.  alt text
alt text

Copy and paste the following command in the Command Prompt Script Editor and leave the Expected time of script execution in seconds to 300. Click the Save button.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -Command "foreach ( $task in ( 'Winget Update All [Logged on User]','Winget Update All [System]' ) ) { try { Get-ScheduledTask | Where-Object { $_.TaskName -eq $task } | Unregister-ScheduledTask -Confirm:$False -ErrorAction Stop } catch { return \"Failed to remove the task '$($task)'. Reason: $($Error[0].Exception.Message)\" } }"

alt text

Enable the Continue on Failure flag. 

alt text

Row 4 Function: Script Log

Add a new row by clicking on Add row button.

alt text

Search and select Script Log Function.

alt text
alt text

Add the following log in the Script Log Message field and click the Save button: Output of command to remove scheduled tasks: %Output%

alt text

Row 5 Logic: If Then Else

alt text
alt text

Row 5a Condition: Output Contains

Type Failed to in the Input Value or Variable field and press Enter.
alt text

Row 5b Function: Set Custom Field

Add a new row by clicking on Add row button.
alt text

Search and select Set Custom Field Function.
alt text
alt text

Search and select WingetUpdateAll_Task_Result in the Search Custom Field field and set Task Deletion Failed in the Value field and click the Save button.
alt text
alt text

Row 5c Function: Set Custom Field

Add a new row by clicking on Add row button in the Else section.

alt text

Search and select Set Custom Field Function.

alt text
alt text

Search and select WingetUpdateAll_Task_Result in the Search Custom Field field and set Task deleted in the Value field and click the Save button.

alt text
alt text

Completed Script

Completed Script

Click the Save button at the top right corner of the screen to save the script.

Save Script

Deployment

It is suggested to run the task once per day against the Winget Update All (Task Delete) device group.

  • Go to Automation > Tasks.
  • Search for Scheduled Task Winget Update All (Delete) Task.
  • Select the concerned task.
  • Click on the Schedule button to schedule the task.

Schedule Task

This screen will appear.

Schedule Screen

Click the Does not repeat button.

Does Not Repeat

This pop-up box will appear.

Pop-Up Box

Click the OK button to save the schedule. Recurrence will be changed to Every day.

Recurrence

Select the Device Groups option in the Targeted Resources section.

Device Groups

Search and select the Winget Update All (Task Delete) device group.

Device Group Selection

Now click the Run button to initiate the task.

Run Task

The task will start appearing in the Scheduled Tasks.

Scheduled Tasks

Output

  • Custom Field
  • Script Log