PowerShell: Prevent screen saver, lock: Move mouse regularly


Alternatively, if you can't change the screen lock settings, you can move the mouse regularly, or have a script move the mouse. Originally published as an AutoIt script, I recreated the script with a few PowerShell lines. Anyone who copies the following commands into a PowerShell session will prevent the computer from locking the screen or starting the screensaver:

Add-Type -AssemblyName System.Windows.Forms
while ($true)
{
  $Pos = [System.Windows.Forms.Cursor]::Position
  [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(($pos.X+1), ($pos.Y+1))
  [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($pos.X, $pos.Y)
  Start-Sleep -Seconds 60
}

To run: Copy content to clipboard and:

The script moves the mouse one pixel and back every 60 seconds.

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2022-12-02 from Bernhard 🔔


Top articles in this section


PowerShell Log-Files: Logging into a textfile - write to file
Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript.

PowerShell Loops and Array
An array stores multiple values, similar to a 2-column table.

PowerShell text file and csv read / write
PowerShell text file and csv read / write

Questions / Comments