# Restart Cursor from a detached process so the caller can exit first. # Usage: # Restart-Cursor.bat # powershell -ExecutionPolicy Bypass -File .\Restart-Cursor.ps1 -DelaySeconds 8 param( [switch]$Worker, [int]$DelaySeconds = 2 ) $ErrorActionPreference = "Stop" $log = Join-Path $PSScriptRoot "Restart-Cursor.log" function Write-Log([string]$Message) { $line = "{0} {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message Add-Content -LiteralPath $log -Value $line -Encoding UTF8 } if (-not $Worker) { $cmd = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -Worker -DelaySeconds $DelaySeconds" $result = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{ CommandLine = $cmd } if ($result.ReturnValue -ne 0) { Write-Log "start worker failed: $($result.ReturnValue)" exit 1 } Write-Log "worker started pid=$($result.ProcessId) delay=$DelaySeconds" Write-Host "Cursor will restart in $DelaySeconds seconds." exit 0 } Start-Sleep -Seconds $DelaySeconds $running = @(Get-CimInstance Win32_Process -Filter "Name = 'Cursor.exe'" | Where-Object { $_.ExecutablePath }) $fallback = "D:\Program Files\cursor\Cursor.exe" if ($running.Count -gt 0) { $exe = $running[0].ExecutablePath } elseif (Test-Path -LiteralPath $fallback) { $exe = $fallback } else { Write-Log "Cursor.exe not found" exit 1 } Write-Log "closing $exe" Get-Process -Name Cursor -ErrorAction SilentlyContinue | ForEach-Object { [void]$_.CloseMainWindow() } $deadline = (Get-Date).AddSeconds(12) while ((Get-Process -Name Cursor -ErrorAction SilentlyContinue) -and (Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 400 } $left = @(Get-Process -Name Cursor -ErrorAction SilentlyContinue) if ($left.Count -gt 0) { Write-Log "force stop $($left.Count)" $left | Stop-Process -Force Start-Sleep -Seconds 1 } Start-Process -FilePath $exe Write-Log "started $exe"