windowspowershellsleep

powershell mouse move does not prevent idle mode


[System.Windows.Forms.Cursor]::Position = `
    New-Object System.Drawing.Point($pos.X, ($pos.Y - 1))
[System.Windows.Forms.Cursor]::Position = `
    New-Object System.Drawing.Point($pos.X, $pos.Y)

Well, I want to move the mouse cursor every 4 minutes to prevent the screensaver from appearing (every second in the code above for testing). The code does really move the mouse every time one pixel up and then down immediately. The thing is, the screensaver (or idle mode of windows) is still appearing.

Where is my mistake?


Solution

  • The solution from the blog Prevent desktop lock or screensaver with PowerShell is working for me. Here is the relevant script, which simply sends a single period to the shell:

    param($minutes = 60)
    
    $myshell = New-Object -com "Wscript.Shell"
    
    for ($i = 0; $i -lt $minutes; $i++) {
      Start-Sleep -Seconds 60
      $myshell.sendkeys(".")
    }