PowerShell Mouse Jiggler

5/5 - (4 votes)

If you have a need to deploy a mouse jiggler (to keep your PC from falling asleep) but don’t have rights to install things on your PC, this is the solution for you!

This script can be run interactively (dot source the function and then run it) or by adding it to your PowerShell profile.

Function Start-MouseJiggler([int]$MoveInSeconds = 15)
{
Add-Type -AssemblyName System.Windows.Forms

# Load Mouse Sonar function
If (-not ([System.Management.Automation.PSTypeName]'MouseSonar').Type)
{
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
public class MouseSonar {
public const uint SPI_SETMOUSESONAR = 0x101D;
public const uint SPIF_UPDATEINIFILE = 0x01;
public const uint SPIF_SENDCHANGE = 0x02;
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SystemParametersInfo (uint uAction, uint uParam, bool lpvParam, uint fuWinIni);
public static void SetSonar (bool Enable) {
SystemParametersInfo(SPI_SETMOUSESONAR, 0, Enable, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
}
'@
}

# Enable Mouse Sonar
[MouseSonar]::SetSonar($true)

# Create coordinates array for mouse. This will limit the mouse to moving around the upper-left hand corner of the
# screen in a 100-pixel square, so you don't have to go looking for it too hard
$xy= @(1..100)

While($True)
{
# Activate Mouse Sonar for current position
[System.Windows.Forms.SendKeys]::SendWait('^')
Start-Sleep -Seconds ($MoveInSeconds/2)
[System.Windows.Forms.Cursor]::Position = (New-Object System.Drawing.Point(($xy | Get-Random),($xy | Get-Random)))
Start-Sleep -Seconds ($MoveInSeconds/2)
# Activate Mouse Sonar for new position
[System.Windows.Forms.SendKeys]::SendWait('^')
}
}

To make it available every time you boot your computer, you can add it to your $Profile (launch PowerShell, run Notepad $PROFILE -Force to create a new profile if you don’t already have one) and paste this function in. Whenever you need to execute it, just pop open a PowerShell window and type:

Start-MouseJiggler

You can modify how often it refreshes by using the -MoveInSeconds parameter. The default is 15 seconds.

Wiggle and jiggle your mouse away!

One Reply to “PowerShell Mouse Jiggler”

  1. Here is mine:

    Clear-Host
    Echo “Moving mouse…”

    $Mouse=@’
    [DllImport(“user32.dll”,CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
    ‘@

    $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)

    $SendMouseClick = Add-Type -memberDefinition $Mouse -name “Win32MouseEventNew” -namespace Win32Functions -passThru
    $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
    $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
    Start-Sleep -Seconds 5
    $PlusOrMinus *= -1
    }

Comments are closed.