Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Powershell Script to keep the mouse moving
Add-Type -AssemblyName System.Windows.Forms
while ($true)
{
$Pos = [System.Windows.Forms.Cursor]::Position
$x = ($pos.X % 500) + 1
$y = ($pos.Y % 500) + 1
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
Start-Sleep -Seconds 10
}
@skswa

This comment has been minimized.

Copy link

@skswa skswa commented Feb 14, 2018

how to run this script

@wylde780

This comment has been minimized.

Copy link

@wylde780 wylde780 commented Feb 14, 2018

@geekloper

This comment has been minimized.

Copy link

@geekloper geekloper commented Dec 27, 2018

In windows 10 :

  • Copy the code in new file with .ps1 extension or download it
  • Right click on file and select Execute with Powershell
@nubamaster228

This comment has been minimized.

Copy link

@nubamaster228 nubamaster228 commented Apr 23, 2019

nice script, man

@AndrewDavis

This comment has been minimized.

Copy link

@AndrewDavis AndrewDavis commented Jun 17, 2019

Clear-Host
Echo "Moving mouse..."
Add-Type -AssemblyName System.Windows.Forms

$PlusOrMinus = 1
while ($true)
{
  $p = [System.Windows.Forms.Cursor]::Position
  $x = $p.X + $PlusOrMinus
  $y = $p.Y + $PlusOrMinus
  [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
  Start-Sleep -Seconds 600
  $PlusOrMinus *= -1
}

Changes:

  • Clears junk and prints out what it's doing
  • 10 minutes (60 * 10) instead of 10 seconds - makes the computer/mouse usable
  • Moves the mouse alternating +-1 - keeps the computer/mouse usable

See also: https://xkcd.com/196/ ^^

@AndrewDavis

This comment has been minimized.

Copy link

@AndrewDavis AndrewDavis commented Jun 17, 2019

Aaaaand that doesn't work for session keep-alive... Had to change it to use Scroll Lock:

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
}

It presses it twice so that it retains its state. (Put it at 60 * 4 minutes = 240 seconds, for those who have 5 minute timeouts.) This seems to work...

https://stackoverflow.com/questions/15835941/powershell-mouse-move-does-not-prevent-idle-mode

@avgawande

This comment has been minimized.

Copy link

@avgawande avgawande commented Feb 28, 2020

Clear-Host
Echo "Moving mouse..."
Add-Type -AssemblyName System.Windows.Forms

$PlusOrMinus = 1
while ($true)
{
  $p = [System.Windows.Forms.Cursor]::Position
  $x = $p.X + $PlusOrMinus
  $y = $p.Y + $PlusOrMinus
  [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($x, $y)
  Start-Sleep -Seconds 600
  $PlusOrMinus *= -1
}

Changes:

  • Clears junk and prints out what it's doing
  • 10 minutes (60 * 10) instead of 10 seconds - makes the computer/mouse usable
  • Moves the mouse alternating +-1 - keeps the computer/mouse usable

See also: https://xkcd.com/196/ ^^

This moves mouse but does not stop my laptop going into sleep

@faisalahmadkhan

This comment has been minimized.

Copy link

@faisalahmadkhan faisalahmadkhan commented May 8, 2020

Guys I am on Windows 10 used to rdp into my system but there is 5 min auto lock which will lock the windows session and take me back to windows lock screen.
how can I avoid this.

I used the mouse movement script above but it required the mouse pointer to be on the same remote session window.

I have to do other tasks on host machine but after 5 min session locks out.

need help

@avgawande

This comment has been minimized.

Copy link

@avgawande avgawande commented May 8, 2020

@AndrewDavis

This comment has been minimized.

Copy link

@AndrewDavis AndrewDavis commented May 8, 2020

How about instead of asking for help you read the thread instead and see the answer already there

@wm-marting

This comment has been minimized.

Copy link

@wm-marting wm-marting commented Sep 13, 2020

Aaaaand that doesn't work for session keep-alive... Had to change it to use Scroll Lock:

[...]

Awesome tip!

@hericksonn

This comment has been minimized.

Copy link

@hericksonn hericksonn commented Sep 24, 2020

This works great! Thanks!

@cody606

This comment has been minimized.

Copy link

@cody606 cody606 commented Feb 2, 2021

I ran mouse move script directly on power shell window, and now mouse keeps on moving. I want help in reverting it.

@stvhwrd

This comment has been minimized.

Copy link

@stvhwrd stvhwrd commented Feb 16, 2021

Thanks for this @AndrewDavis! ScrollLock is a perfect key choice for my use case. I had previously been using Ctrl ("^") but I like that the ScrollLock light flashes on my keyboard -- it reminds me that this script is running.

Here's my slightly modified version with some timestamps and a heartbeat:

#
# -- NoSleep --
# Keep your computer awake by programmatically pressing the ScrollLock key every X seconds
#

param($sleep = 240) # seconds
$announcementInterval = 10 # loops

Clear-Host

$WShell = New-Object -com "Wscript.Shell"
$date = Get-Date -Format "dddd MM/dd HH:mm (K)"

$stopwatch
# Some environments don't support invocation of this method.
try {
    $stopwatch = [system.diagnostics.stopwatch]::StartNew()
} catch {
   Write-Host "Couldn't start the stopwatch."
}

Write-Host "Executing ScrollLock-toggle NoSleep routine."
Write-Host "Start time:" $(Get-Date -Format "dddd MM/dd HH:mm (K)")

Write-Host "<3" -fore red

$index = 0
while ( $true )
{
    Write-Host "< 3" -fore red      # heartbeat
    $WShell.sendkeys("{SCROLLLOCK}")

    Start-Sleep -Milliseconds 200

    $WShell.sendkeys("{SCROLLLOCK}")
    Write-Host "<3" -fore red       # heartbeat

    Start-Sleep -Seconds $sleep

    # Announce runtime on an interval
    if ( $stopwatch.IsRunning -and (++$index % $announcementInterval) -eq 0 )
    {
        Write-Host "Elapsed time: " $stopwatch.Elapsed.ToString('dd\.hh\:mm\:ss')
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment