Uninstalling Elastic Agent 8.8.2 on Windows Devices

I’m attempting to uninstall Elastic Agent version 8.8.2 from Windows devices using a script. While the script successfully uninstalls the software on the first attempt, it doesn’t delete the installation folder. I have to run the script a second time to remove the folder. Could anyone assist me in modifying the script so that it uninstalls the software and removes the folder in one go?

$processes=@(

'filebeat',

'metricbeat',

'fleet-server',

'elastic-endpoint',

'elastic-agent'

)

$runningProcesses = Get-Process | select ProcessName

foreach ($process in $processes)

    {

    if ($process -in $runningProcesses.ProcessName)

        {

        Write-Output "$process found"

        Stop-Process -Name $process -Force -ErrorAction SilentlyContinue

        }

    }

if (Test-Path "$($env:ProgramFiles)\elastic\agent\elastic-agent.exe")

    {

    Start-Sleep -Seconds 5

    $directoryName = Get-ChildItem -Path "C:\Program Files\Elastic\Agent\data\" | where {$_.name -like "elastic*"}

    Start-Process -FilePath "$($env:ProgramFiles)\elastic\agent\data\$($directoryName.Name)\elastic-agent.exe" -ArgumentList "uninstall --force"

    Start-Sleep -Seconds 5

    }

if (Test-Path -Path "C:\Program Files\Elastic")

    {

    Write-Output "Path still present - attempting removal."

    Remove-Item -Path "C:\Program Files\Elastic\Agent" -Recurse -Force -ErrorAction SilentlyContinue

    }

 

any update ?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.