Looking for powershell script to uninstall Rapid7 agent

  • 1 February 2024
  • 2 replies


Looking for a PowerShell script to uninstall the Rapid7 agent from Windows. I used Automox Otto AI to create one (see below), but when I run it, I keep getting “Rapid7 Insight Agent is not installed on this machine.” I checked the system, and the software is installed, so I know it exists. 


# Otto AI Generated Code
# ======================
# Uninstall Rapid7 Insight Agent
$uninstallString = Get-ItemProperty -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object {$_.DisplayName -like "*Rapid7 Insight Agent*"} | Select-Object -ExpandProperty UninstallString

if ($uninstallString) {
    Write-Host "Uninstalling Rapid7 Insight Agent..."
    Start-Process -FilePath "msiexec.exe" -ArgumentList "/x $uninstallString /qn" -Wait
    Write-Host "Rapid7 Insight Agent has been uninstalled."
else {
    Write-Host "Rapid7 Insight Agent is not installed on this machine."

2 replies

Userlevel 1

# Check if the Rapid7 Insight Agent is installed
$agentInstalled = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -eq "Rapid7 Insight Agent" }

if ($agentInstalled) {
    Write-Output "Rapid7 Insight Agent found. Uninstalling..."

    # Uninstall the Rapid7 Insight Agent
    $uninstallResult = msiexec.exe /x $($agentInstalled.IdentifyingNumber) /qn

    if ($uninstallResult -eq 0) {
        Write-Output "Rapid7 Insight Agent has been successfully uninstalled."
    } else {
        Write-Output "Failed to uninstall Rapid7 Insight Agent. Exit code: $($uninstallResult)"
} else {
    Write-Output "Rapid7 Insight Agent is not installed on this system."

Userlevel 5
Badge +1

Hey @anuj.johri that is happening because you are trying to query a 64-bit registry key using a 32-bit powershell process. The code below will 

# ScriptBlock to Find Software
$scriptblock = {
$softwareName = "Rapid7 Insight Agent"
$uninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Get-ChildItem $uninstallKey | Get-ItemProperty | Where-Object { $_.DisplayName -match $softwareName}

# Query Software
$softwareKey = @(& "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptBlock)

# Get Uninstall String
$uninstallString = $softwareKey.UninstallString
$uninstallString = $uninstallString.Replace("MsiExec.exe /I","/X ")
$uninstallString = $uninstallString.Replace("MsiExec.exe /X","/X ")

# Run Uninstall
Start-Process -FilePath MsiExec.exe -ArgumentList "$uninstallString /qn" -Wait

# Validate
remove-variable softwareKey
$softwareKey = @(& "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptBlock)
Write-Output "Failed to remove with: MsiExec.exe $uninstallString /qn"
Write-Output "Rapid7 Removed Successfully"