## Get the NinjaRMM installation directory
If (Test-Path "${Env:ProgramFiles(x86)}") {
    $INSTALLDIR = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\NinjaRMM LLC\NinjaRMMAgent').Location
} Else {
    $INSTALLDIR = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\NinjaRMM LLC\NinjaRMMAgent').Location
}

## Stop the Ninja processes and service
Stop-Process -Name NinjaRMMAgent
Stop-Process -Name NinjaRMMAgentPatcher

## Run Ninja's uninstaller
If ( Test-Path "${INSTALLDIR}\uninstall.exe") {
    Start-Process -FilePath "${INSTALLDIR}\uninstall.exe" -ArgumentList "--mode unattended" -Wait
}

## Clean up remaining service registry entries
Start-Process -FilePath "sc.exe" -ArgumentList "delete NinjaRMMAgent"

## Clean up remaining directories
If (Test-Path "${INSTALLDIR}") { Remove-Item -Path "${INSTALLDIR}" -Recurse -Force }
If (Test-Path "${Env:ProgramData}\NinjaRMMAgent") { Remove-Item -Path "${Env:ProgramData}\NinjaRMMAgent" -Recurse -Force }

## Clean up remaining registry entries
If ( Test-Path $(Split-Path -Path $WOW64RegKey -Parent) ) { Remove-Item -Path $(Split-Path -Path $WOW64RegKey -Parent) -Recurse -Force }
If ( Test-Path $(Split-Path -Path $RegKey -Parent) ) { Remove-Item -Path $(Split-Path -Path $RegKey -Parent) -Recurse -Force }

## Remove the NinjaRMMAgent service
Start-Process "sc.exe" -ArgumentList "delete NinjaRMMAgent" -Wait