Before I start, here is my very first little code I wrote in PowerShell :)
[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)
What do I want to achieve?
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.
Now, I am learning PowerShell and I have little experience with the Windows architecture.
Does anybody see my mistake? I would appreciate an answer a lot! :D Thanks in advance.
To move the cursor one character to the left, press the Left arrow . To move the cursor one word to the left, press Ctrl + Left arrow . To move the cursor one character to the right, press the Right arrow . To move the cursor one word to the right, press Ctrl + Right arrow .
Go to Control Panel > Personalization > Change Screensaver. Next to On Resume, Display Logon Screen, uncheck the box. This prevents your system from sleeping.
Open a PowerShell console session, type exit , and press the Enter key. The PowerShell console will immediately close. This keyword can also exit a script rather than the console session.
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(".") }
and an alternative from the comments, which moves the mouse a single pixel:
$Pos = [System.Windows.Forms.Cursor]::Position [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) + 1) , $Pos.Y) $Pos = [System.Windows.Forms.Cursor]::Position [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point((($Pos.X) - 1) , $Pos.Y)
I tried a mouse move solution too, and it likewise didn't work. This was my solution, to quickly toggle Scroll Lock every 4 minutes:
Clear-Host Echo "Keep-alive with Scroll Lock..." $WShell = New-Object -com "Wscript.Shell" while ($true) { $WShell.sendkeys("{SCROLLLOCK}") Start-Sleep -Milliseconds 100 $WShell.sendkeys("{SCROLLLOCK}") Start-Sleep -Seconds 240 }
I used Scroll Lock because that's one of the most useless keys on the keyboard. Also could be nice to see it briefly blink every now and then. This solution should work for just about everyone, I think.
Some people get success using $WShell.sendkeys("SCROLLLOCK") instead of $WShell.sendkeys("{SCROLLLOCK}")
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With