Clean uninstall of PowerSyncPro Migration Agent
There may be times where you need or want a complete uninstall of PowerSyncPro Migration Agent. The following script will achieve this for you. You can use this script stand-alone, or incorporate it into your software deployment tools and also Intune as a Platform Script under Scripts and remediations.
It will:
Check for the presence of PowerSyncPro
- Stop the Service
- Find the correct UninstallString
- Uninstall PowerSyncPro Migration Agent
- Delete the associated ProgramData directory
- Delete the associated PowerSyncPro Registry Keys
- Write a txt Log in c:\ProgramData
- Write entries to the Windows Event Log
$RegPath = 'HKLM:\SOFTWARE\Declaration Software\'
$UninstallKeyPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
$ServiceName = 'PowerSyncPro Migration Agent'
$AppName = 'PowerSyncPro Migration Agent'
$AppDataPath = 'C:\ProgramData\Declaration Software\'
$LogFile = "C:\ProgramData\PowerSyncProCleanup.log"
$EventSource = "Intune PowerSyncPro Cleanup"
function Log-Message {
param (
[string]$Message,
[string]$EventType = "Information"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "$timestamp - $Message"
# Write to file
Add-Content -Path $LogFile -Value $logEntry
# Attempt to log to Windows Event Log
try {
if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) {
New-EventLog -LogName Application -Source $EventSource -ErrorAction SilentlyContinue
}
Write-EventLog -LogName Application -Source $EventSource -EntryType $EventType -EventId 1000 -Message $Message
} catch {
# If we can't write to event log (common in Intune SYSTEM context), just skip
}
}
# Begin script
Log-Message "Starting PowerSyncPro cleanup..."
# Only proceed if the key exists
if (Test-Path $RegPath) {
try {
# Stop service if it exists
$svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
if ($svc) {
Stop-Service -Name $ServiceName -Force -ErrorAction Stop
Log-Message "Service '$ServiceName' stopped successfully."
} else {
Log-Message "Service '$ServiceName' not found." -EventType "Warning"
}
# Look for the uninstall string
$UninstallKey = Get-ChildItem $UninstallKeyPath |
Where-Object { $_.GetValue('DisplayName') -eq $AppName }
if ($UninstallKey) {
$UninstallString = $UninstallKey.GetValue('UninstallString')
if ($UninstallString) {
$cmd = "$UninstallString /norestart /quiet"
Log-Message "Uninstalling: $cmd"
Start-Process -FilePath "cmd.exe" -ArgumentList "/c $cmd" -Wait
Log-Message "Uninstallation completed."
} else {
Log-Message "Uninstall string not found." -EventType "Warning"
}
} else {
Log-Message "Uninstall key not found in registry." -EventType "Warning"
}
# Remove registry key
Remove-Item -Path $RegPath -Recurse -Force -ErrorAction Stop
Log-Message "Registry key '$RegPath' deleted."
# Remove application data folder
if (Test-Path $AppDataPath) {
Remove-Item -Path $AppDataPath -Recurse -Force -ErrorAction Stop
Log-Message "AppData folder '$AppDataPath' deleted."
} else {
Log-Message "AppData folder '$AppDataPath' not found." -EventType "Warning"
}
# Final check
if (-not (Test-Path $RegPath)) {
Log-Message "Cleanup successful. Exiting with code 0."
exit 0
} else {
Log-Message "Registry key still exists. Cleanup incomplete." -EventType "Error"
exit 1
}
} catch {
Log-Message "An error occurred: $_" -EventType "Error"
exit 1
}
} else {
Log-Message "Registry key not found. No action taken."
exit 0
}