Skip to main content

Remove Application

Summary

This script is designed to perform the uninstallation of the application provided in the parameter. The script requires the application name(s) to precisely match those listed under the Applications tile in the device available in CW RMM.

Sample Run

Sample Run 1
Sample Run 2
Sample Run 3

Dependencies

Custom Field - Remove Application Result

User Parameters

NameExampleRequiredDescription
ApplicationName'Google Chrome', 'Microsoft OneDrive', 'VMware Tools'TrueThe name of the application(s) to remove is encoded in single quotation marks. When specifying multiple application names, ensure they are separated by a comma (,) without any spaces, with each application encoded in single quotation marks, as depicted in the example.

Implementation

Create Task

Remove Application: To implement this script, please create a new "PowerShell" style script in the system.
Create Task

Name: Remove Application
Description: This script is designed to perform the uninstallation of the application provided in the parameter. The script requires the application name(s) to precisely match those listed under the Applications tile in the device available in CW RMM.
Category: Custom

Task Category

Script

Start by making three separate rows. You can do this by clicking the "Add Row" button at the bottom of the script page.
Add Row

Row 1: Function: Script Log

Row 1 Log

Paste the highlighted text:
Validating the application name(s) to precisely match in the list of installed applications and bloatware programs detected on the computer.
Row 1 Log Image

Row 2: Function: PowerShell Script

Row 2 PowerShell
Row 2 PowerShell Image

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

$ErrorActionPreference = 'SilentlyContinue'
$to_Remove = @ApplicationName@
if ($to_Remove) {
$Applications = @()
$for_removal = @()
$Applications = [string[]]($(Get-AppxPackage).Name) + [string[]]((Get-AppxProvisionedPackage -online).DisplayName) + [string[]]((Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Get-ItemProperty).DisplayName); $for_removal = foreach ($application in $applications) { if ( $to_remove -contains $application) { $application } };
if ($for_removal) { return " '$($for_removal -join ''',''')' " } else { return 'Not Installed' }
}
else {
return 'Parameter missing'
}

Row 3: Function: Script Log

Row 3 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.
Row 3 Log Image

Row 4: Logic: If/Then/Else

Row 4 Logic
Row 4 Logic Image

Row 4a: Condition: Output Contains

In the IF part, enter Not Installed in the right box of the "Output Contains" part.
Add Condition OR
Enter Parameter Missing in the right box of the "Output Contains" part.
Row 4a Condition

Row 4b: Function: Script Log

Row 4b Log

Paste the highlighted text:
Application Name does not exist. Exiting script.
Row 4b Log Image

Row 4c: Function: Script Exit

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

In the script exit message, leave it blank.
Image

Row 5: Function: Script Log

Row 5 Log

Paste the highlighted text:
Executing the PowerShell script to perform the uninstallation of the @ApplicationName@.

Image

Row 6: Function: PowerShell Script

Row 6 PowerShell

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

#region Setup - Variables
$ProjectName = 'Remove-Application'
[Net.ServicePointManager]::SecurityProtocol = [enum]::ToObject([Net.SecurityProtocolType], 3072)
# # Parameters and Globals
# # Be sure that the name of the hashtable property matches the name of the parameter of the script that you are calling.
$Parameters = @{
Name = @ApplicationName@
}
$BaseURL = 'https://file.provaltech.com/repo'
$PS1URL = "$BaseURL/script/$ProjectName.ps1"
$WorkingDirectory = "C:\ProgramData\_automation\script\$ProjectName"
$PS1Path = "$WorkingDirectory\$ProjectName.ps1"
$WorkingPath = $WorkingDirectory
#endregion
#region Setup - Folder Structure
mkdir -Path $WorkingDirectory -ErrorAction SilentlyContinue | Out-Null
$response = Invoke-WebRequest -Uri $PS1URL -UseBasicParsing
if (($response.StatusCode -ne 200) -and (!(Test-Path -Path $PS1Path))) {
Write-Error -Message "No pre-downloaded script exists and the script '$PS1URL' failed to download. Exiting."
return
} elseif ($response.StatusCode -eq 200) {
Remove-Item -Path $PS1Path -ErrorAction SilentlyContinue
[System.IO.File]::WriteAllLines($PS1Path, $response.Content)
}
if (!(Test-Path -Path $PS1Path)) {
Write-Error -Message 'An error occurred and the script was unable to be downloaded. Exiting.'
return
}
#endregion
#region Execution
& $PS1Path @Parameters
#endregion

Row 7: Function: Script Log

Row 7 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.
Row 7 Log Image

Row 8: Function: PowerShell Script

Row 8 PowerShell
Row 8 PowerShell Image

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

$ErrorActionPreference = 'SilentlyContinue'
$to_Remove = @ApplicationName@
if ($to_Remove) {
$Applications = @()
$for_removal = @()
$Applications = [string[]]($(Get-AppxPackage).Name) + [string[]]((Get-AppxProvisionedPackage -online).DisplayName) + [string[]]((Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*', 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Get-ItemProperty).DisplayName); $for_removal = foreach ($application in $applications) { if ( $to_remove -contains $application) { $application } };
if ($for_removal) { return " '$($for_removal -join ''',''')' " } else { return 'Not Installed' }
}
else {
return 'Parameter missing'
}

Row 9: Function: Script Log

Row 9 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.
Row 9 Log Image

Row 10: Logic: If/Then/Else

Row 10a: Condition: Output Contains

In the IF part, enter Not Installed in the right box of the "Output Contains" part.
Row 10a Condition

Row 10b: Function: Script Log

Row 10b Log

Paste the highlighted text:
@ApplicationName@ successfully uninstalled. Exiting script.
Row 10b Log Image

Row 10c: Function: Set Custom Field

Add a new row by clicking on the Add Row button.
Select Function 'Set Custom Field'. When you select set custom field, it will open up a new window.

In this window, search for the Remove Application Result field.
Custom Field: Remove Application Result
Value: Success

Image

Row 10d: Function: Script Exit

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

In the script exit message, leave it blank.
Image

Row 11: Function: Script Exit

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

In the script exit message, write @ApplicationName@ failed to uninstall. Exiting script with error.
Row 11 Exit

Row 12: Function: Set Custom Field

Add a new row by clicking on the Add Row button.
Select Function 'Set Custom Field'. When you select set custom field, it will open up a new window.

In this window, search for the Remove Application Result field.
Custom Field: Remove Application Result
Value: Failed
Row 12 Set Custom Field

Row 13: Complete

Row 13 Complete 1
Row 13 Complete 2

Deployment

It is suggested to run the Task every 2 hours against the group Remove Application.

  • Go to Automation > Tasks.
  • Search for Remove Application Task.
  • Select the concerned task.
  • Click on the Schedule button to schedule the task/script.
    Schedule Task

This screen will appear.
Schedule Screen

Select the relevant time to run the script and click the Do not repeat button.
Select Time

A pop-up box will appear.
Change the number of hours to 2 and click OK.
Change Hours
Change Hours Confirmation

Select the target to schedule at site 'ProVal - Development'.
For example:
Select Target

Now click the Run button once all customization is set to initiate the task.
Run Task

The task will start appearing in the Scheduled Tasks.

Output

  • Task log
  • Custom field 'Remove Application Result'