$FolderToRename = "${Env:ProgramFiles}\ATERA Networks\AteraAgent\Packages\AgentPackageMonitoring"
$RenameSuffix = "-Old"
$FolderNewName = "${FolderToRename}${RenameSuffix}"

# Kill processes that can interfere
'reg', 'AgentPackageTaskManagement' | ForEach-Object { Get-Process -Name $_ -ErrorAction SilentlyContinue | Stop-Process -Force }

Write-Output "Stopping Atera agent"
Stop-Service -Name 'AteraAgent' -Force

If ( Test-Path $FolderNewName ) {
    Write-Output "Deleting previously renamed folder: ${FolderNewName}"
    Try { Remove-Item $FolderNewName -Recurse -Force }
    Catch { Write-Error $_.Exception.Message }
}

Write-Output "Stopping AgentPackageMonitoring process"
Get-Process -Name "AgentPackageMonitoring" -ErrorAction SilentlyContinue | Stop-Process -Force

Write-Output "Renaming ${FolderToRename}"
Try { Rename-Item $FolderToRename $FolderNewName -Force }
Catch { Write-Error $_.Exception.Message }

Write-Output "Starting Atera agent"
Start-Service -Name 'AteraAgent'