PS script for antivirus removal

Hey Guys trying to get some help with script below. Can’t get script to work. task scheduler is show a last run result (0x1)

$uninstallCommand = “C:\Program Files\Sophos\Sophos Endpoint Agent\SophosUninstaller.exe /S”
$action = New-ScheduledTaskAction -Execute “powershell.exe” -Argument “-NoProfile -ExecutionPolicy Bypass -Command "$uninstallCommand”"
$trigger = New-ScheduledTaskTrigger -Once -At “2:50PM”
$principal = New-ScheduledTaskPrincipal -UserId “SYSTEM” -LogonType ServiceAccount
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
Register-ScheduledTask -TaskName “Silent Uninstall Sophos” -Action $action -Trigger $trigger -Principal $principal -Settings $settings
Write-Output “Scheduled task ‘Silent Uninstall Sophos’ created successfully.”

it would help everyone to read this if you formatted your code using the “Preformatted Text” button in the editing window. Sometimes it’s inside the gear icon.

Beyond that, this is probably a syntax issue with trying to execute an executable with a parameter but passing it all as powershell variables while making a scheduled task.
To remove some complexity try changing your action line to:

$action = New-ScheduledTaskAction -Execute "C:\Program Files\Sophos\Sophos Endpoint Agent\SophosUninstaller.exe" - Argument "/S"

That way the executable that the Scheduled task is triggering is the uninstaller you want to run anyway, and the arguments should end up in the appropriate field when double-checking in the GUI.

1 Like