The following PowerShell script is designed to move the mouse cursor randomly on the screen.
On this pageJump to a section
This can be useful for preventing your computer from entering sleep mode or locking the screen due to inactivity.
Prerequisites
- Windows OS: This script is designed for Windows operating systems – such as Windows 10 or Windows 11
- PowerShell: Make sure PowerShell is installed and accessible on your machine.
PowerShell Script
Add-Type -assemblyName System.Windows.Forms;$a=@(1..100);while(1){[System.Windows.Forms.Cursor]::Position=New-Object System.Drawing.Point(($a|get-random),($a|get-random));start-sleep -seconds 5}
How does this work?
- Add-Type -assemblyName System.Windows.Forms: Adds the System.Windows.Forms assembly to access the
Cursorclass. - $a=@(1..100): Creates an array with integers from 1 to 100.
- while(1): Infinite loop to keep the script running.
- [System.Windows.Forms.Cursor]::Position: Sets the mouse cursor’s position.
- New-Object System.Drawing.Point(($a|get-random),($a|get-random)): Randomly selects an x, y coordinate from the array
$a. - start-sleep -seconds 5: Pauses the script for 5 seconds before running the loop again.
How to use the Script
- Open PowerShell: Open PowerShell.
- Copy and Paste: Copy the one-liner script and paste it into the PowerShell window.
- Execute: Press Enter to execute the script.
Your mouse cursor will now move to a random position every 5 seconds.
To Stop the Script
- Close PowerShell Window: Simply close the PowerShell window where the script is running.
Caution
- Use Responsibly: This script will move the cursor indefinitely until stopped.