How to uninstall Kaseya using PowerShell

How to uninstall Kaseya using PowerShell

How to uninstall Kaseya using PowerShell. This Powershell Script will help you remove Kaseya Agent installations on Windows systems, the following PowerShell script is designed to automate the process. It ensures the Kaseya Agent and associated performance counters are uninstalled and all residual files are removed.

Kaseya PowerShell Removal Script

# PatrickDomingues.com
# Define the service names for Kaseya Agent and Kaseya Agent Endpoint
$agentService = "Kaseya Agent"
$endpointService = "Kaseya Agent Endpoint"

# Stop the Kaseya services if they are running
Write-Output "Stopping Kaseya services..."
if (Get-Service -Name $agentService -ErrorAction SilentlyContinue) {
    Stop-Service -Name $agentService -Force
}
if (Get-Service -Name $endpointService -ErrorAction SilentlyContinue) {
    Stop-Service -Name $endpointService -Force
}

# Get the specific Kaseya agent directory name
$kdir = Get-Childitem 'C:\Program Files (x86)\Kaseya' | Select-Object -ExpandProperty Name

# Silently uninstall Kaseya Agent
Write-Output "Uninstalling Kaseya Agent..."
Start-Process -FilePath "C:\Program Files (x86)\Kaseya\$kdir\KASetup.exe" -ArgumentList "/s","/r","/g $kdir","/l %temp%\kasetup.log" -Wait

# Delete leftover performance counters
$csvfiles = Get-ChildItem 'C:\kworking\Klogs' | Select-Object -ExpandProperty Name | ForEach-Object { $_.replace('.csv', '') }
$counters = foreach ($a in $csvfiles) { $a.replace('KLOG', 'KCTR') }

ForEach ($i in $counters) {
    Write-Output "Removing performance counter $i..."
    &logman.exe stop $i
    &logman.exe delete $i
}

# Remove the kworking folder
Write-Output "Removing kworking folder..."
&cmd.exe /c rd /s /q c:\kworking

Write-Output "Kaseya Agent uninstallation and cleanup complete."

Kaseya PowerShell Removal Script Explanation

Below is a step-by-step breakdown of how the script operates:

Step 1: Define Service Names

The script starts by defining the names of the two Kaseya services targeted for removal:

powershellCopy code$agentService = "Kaseya Agent"
$endpointService = "Kaseya Agent Endpoint"

Step 2: Stop Kaseya Services

To avoid conflicts during uninstallation, it stops both services if they are running. This is done using Get-Service to check each service status and Stop-Service -Force to terminate them if they are active:

powershellCopy codeWrite-Output "Stopping Kaseya services..."
if (Get-Service -Name $agentService -ErrorAction SilentlyContinue) {
    Stop-Service -Name $agentService -Force
}
if (Get-Service -Name $endpointService -ErrorAction SilentlyContinue) {
    Stop-Service -Name $endpointService -Force
}

Step 3: Locate the Kaseya Agent Directory

Next, the script retrieves the specific directory under C:\Program Files (x86)\Kaseya where Kaseya Agent files are stored. This ensures the correct path is used for uninstallation:

powershellCopy code$kdir = Get-Childitem 'C:\Program Files (x86)\Kaseya' | Select-Object -ExpandProperty Name

Step 4: Silently Uninstall the Kaseya Agent

Using the path determined earlier, the script initiates a silent uninstallation of the Kaseya Agent by launching the KASetup.exe file within the Kaseya directory with specific arguments:

  • /s initiates a silent uninstall.
  • /r and /g provide configurations specific to the Kaseya environment.
  • /l logs the process to a temporary file for troubleshooting purposes.
powershellCopy codeWrite-Output "Uninstalling Kaseya Agent..."
Start-Process -FilePath "C:\Program Files (x86)\Kaseya\$kdir\KASetup.exe" -ArgumentList "/s","/r","/g $kdir","/l %temp%\kasetup.log" -Wait

Step 5: Delete Performance Counters

The script then removes any leftover performance counters, a step that helps to prevent unnecessary data from being tracked once the agent is uninstalled. It does this by reading .csv files from the C:\kworking\Klogs directory, adjusting the filenames to match the Kaseya counter naming conventions, and using logman.exe to stop and delete each counter.

powershellCopy code$csvfiles = Get-ChildItem 'C:\kworking\Klogs' | Select-Object -ExpandProperty Name | ForEach-Object { $_.replace('.csv', '') }
$counters = foreach ($a in $csvfiles) { $a.replace('KLOG', 'KCTR') }

ForEach ($i in $counters) {
    Write-Output "Removing performance counter $i..."
    &logman.exe stop $i
    &logman.exe delete $i
}

Step 6: Remove the kworking Folder

Finally, the script removes the kworking folder using cmd.exe with the /s /q flags, which delete all files and subdirectories without prompting:

powershellCopy codeWrite-Output "Removing kworking folder..."
&cmd.exe /c rd /s /q c:\kworking

Completion Message

The script concludes by outputting a message that the Kaseya Agent uninstallation and cleanup process is complete:

powershellCopy codeWrite-Output "Kaseya Agent uninstallation and cleanup complete."

This PowerShell script provides a streamlined and thorough approach to completely removing the Kaseya Agent and related files, ensuring no residual data remains that could impact system performance or future software installations.


Discover more from Patrick Domingues

Subscribe to get the latest posts sent to your email.

author avatar
Patrick Domingues

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.