# 1. Locate NinjaRMMAgent installation path via registry $UninstallKey = Get-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\NinjaRMMAgent*" -ErrorAction SilentlyContinue if (-not $UninstallKey) { $UninstallKey = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\NinjaRMMAgent*" -ErrorAction SilentlyContinue } if ($UninstallKey) { # Extract directory from the UninstallString path $UninstallPath = Split-Path -Path ($UninstallKey.UninstallString -replace '"','') -Parent # 2. Disable Uninstall Prevention (Restores uninstall.exe if hidden/tamper-protected) $AgentExe = Join-Path $UninstallPath "NinjaRMMAgent.exe" if (Test-Path $AgentExe) { Start-Process -FilePath $AgentExe -ArgumentList "-disableUninstallPrevention" -Wait -WindowStyle Hidden } # 3. Execute silent uninstallation $Uninstaller = Join-Path $UninstallPath "uninstall.exe" if (Test-Path $Uninstaller) { Start-Process -FilePath $Uninstaller -ArgumentList "--mode unattended" -Wait -WindowStyle Hidden } } else { # Fallback to MSI product code removal if registry key path isn't found $NinjaProduct = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like "*NinjaRMMAgent*" } if ($NinjaProduct) { $NinjaProduct.Uninstall() | Out-Null } } # 4. Stop and remove lingering services $Services = @("NinjaRMMAgent", "NinjaRMMAgentPatcher", "NinjaRMM-NMS") foreach ($Svc in $Services) { if (Get-Service -Name $Svc -ErrorAction SilentlyContinue) { Stop-Service -Name $Svc -Force -ErrorAction SilentlyContinue sc.exe delete $Svc | Out-Null } } # 5. Clean up lingering directories $PathsToClean = @( "C:\ProgramData\NinjaRMMAgent", "C:\Program Files (x86)\NinjaRMMAgent" ) # Append dynamic organization directory if detected if ($UninstallPath -and (Test-Path $UninstallPath)) { $PathsToClean += $UninstallPath } foreach ($Path in $PathsToClean) { if (Test-Path $Path) { Remove-Item -Path $Path -Recurse -Force -ErrorAction SilentlyContinue } }